Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > moving script from body to head

Reply
Thread Tools

moving script from body to head

 
 
toby989@hotpop.com
Guest
Posts: n/a
 
      09-28-2006
Hi

I am very new to javascript and html. My question is how can I move the script
from the body section to the head section without affecting how the result of
the htm looks?
In the example below, I guess the document.write() dumps its results at the
location where it is.

Thanks for your hints.

Toby



<html>
<body>

<script type="text/javascript">

// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")

// Transform
document.write(xml.transformNode(xsl))

</script>

</body>
</html>
 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-28-2006
said the following on 9/28/2006 1:45 PM:
> Hi
>
> I am very new to javascript and html. My question is how can I move the
> script from the body section to the head section without affecting how
> the result of the htm looks?
> In the example below, I guess the document.write() dumps its results at
> the location where it is.


Yes, that is precisely what it does.

> Thanks for your hints.


Hints:

Object/Feature detect before using an object.

Functionalize your code and use a function call in the body (overkill to
me for what you are doing).


>
>
> <html>
> <body>
>
> <script type="text/javascript">
>
> // Load XML
> var xml = new ActiveXObject("Microsoft.XMLDOM")


And if the browser doesn't support ActiveX or Microsoft.XMLDOM?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
 
toby989@hotpop.com
Guest
Posts: n/a
 
      09-28-2006
Randy Webb wrote:
> said the following on 9/28/2006 1:45 PM:
>
>> Hi
>>
>> I am very new to javascript and html. My question is how can I move
>> the script from the body section to the head section without affecting
>> how the result of the htm looks?
>> In the example below, I guess the document.write() dumps its results
>> at the location where it is.

>
>
> Yes, that is precisely what it does.
>
>> Thanks for your hints.

>
>
> Hints:
>
> Object/Feature detect before using an object.

Will do.
>
> Functionalize your code and use a function call in the body (overkill to
> me for what you are doing).

Then..... how to move script from the head to the body?
<html>
<head>
<style>
div {border: 1px solid black;}
</style>
<title>XPath - JavaScript Tests</title>
<script>
var xslStylesheet;
var xsltProcessor = new XSLTProcessor();
var myDOM;
var xmlDoc;
function Init() {
// load the xslt file, example1.xsl
var myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", "example1.xsl", false);
myXMLHTTPRequest.send(null);
xslStylesheet = myXMLHTTPRequest.responseXML;
xsltProcessor.importStylesheet(xslStylesheet);
// load the xml file, example1.xml
myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", "example1.xml", false);
myXMLHTTPRequest.send(null);
xmlDoc = myXMLHTTPRequest.responseXML;
var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById("example").innerHTML = "";
myDOM = fragment;
document.getElementById("example").appendChild(fra gment)
}
</script>
</head>
<body>
<a href="javascript:Init()">Run XSLT</a>
<div id="example">
</div>
</body>
</html>
Dont want to show contents only after 'click', but immediately, just like the
version written for the IE.


>
>
>>
>>
>> <html>
>> <body>
>>
>> <script type="text/javascript">
>>
>> // Load XML
>> var xml = new ActiveXObject("Microsoft.XMLDOM")

>
>
> And if the browser doesn't support ActiveX or Microsoft.XMLDOM?
>

 
Reply With Quote
 
agapeton@gmail.com
Guest
Posts: n/a
 
      09-29-2006
First off... ActiveX is not allowed on the Internet. That's only for
Intranet Explorer.

Secondly... here... use this...

function CreateXmlHttpObject( ) {
var xmlhttp;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(ex) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(except) {
xmlhttp = false;
}
}

if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest( );
}

return xmlhttp;
}

It returns and XmlHttp object that you can use for whatever...

Next... document.write died almost 10 years ago.

Finally, unless you are using XHTML 1.1 (which IE6.0 can't even support
because you really need to use the application/xhtml+xml content type
for XHTML 1.1 and IE6.0 is retarded with it), then you won't be
screwing with the body content.

I would HIGHLY suggest that you learn DOM programming... if you're a
Microsoft person, you'll be learning DOM programming anyhow because
Microsoft's WPF is based on all those awesome concepts. Get a head
start...

wrote:
> Hi
>
> I am very new to javascript and html. My question is how can I move the script
> from the body section to the head section without affecting how the result of
> the htm looks?
> In the example below, I guess the document.write() dumps its results at the
> location where it is.
>
> Thanks for your hints.
>
> Toby
>
>
>
> <html>
> <body>
>
> <script type="text/javascript">
>
> // Load XML
> var xml = new ActiveXObject("Microsoft.XMLDOM")
> xml.async = false
> xml.load("cdcatalog.xml")
>
> // Load XSL
> var xsl = new ActiveXObject("Microsoft.XMLDOM")
> xsl.async = false
> xsl.load("cdcatalog.xsl")
>
> // Transform
> document.write(xml.transformNode(xsl))
>
> </script>
>
> </body>
> </html>


 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      09-29-2006
said the following on 9/28/2006 11:41 PM:
> First off... ActiveX is not allowed on the Internet. That's only for
> Intranet Explorer.


Who fed you that line of crap? ActiveX is "allowed on the Internet" (If
it's not, please tell me who enforces that stupid belief). It may not be
supported though.

> Secondly... here... use this...


Wait, you said ActiveX is not allowed on the Internet and then you post
code to use on the Internet that uses ActiveX?

<snip>

> It returns and XmlHttp object that you can use for whatever...


No, it attempts to return an XMLHttp object.

> Next... document.write died almost 10 years ago.


No it didn't.

> Finally, unless you are using XHTML 1.1 (which IE6.0 can't even support
> because you really need to use the application/xhtml+xml content type
> for XHTML 1.1 and IE6.0 is retarded with it), then you won't be
> screwing with the body content.


Say what? Can't "mess with the body content"? Sure you can. You just
don't use document.write or innerHTML to do it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
More than a single script block within a single HEAD and BODY Water Cooler v2 Javascript 2 04-18-2006 11:00 AM
GeForce 7800 GTX Head-to-Head @ TrustedReviews Silverstrand Front Page News 0 09-12-2005 11:25 PM
PHP and ASP.NET go HEAD to HEAD showme ASP .Net 13 07-10-2004 09:44 PM
Injecting code into the <head></head> section Brian W ASP .Net 10 07-02-2003 07:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57