By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,518 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

moving script from body to head

P: n/a
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>
Sep 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
to*****@hotpop.com 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/
Sep 28 '06 #2

P: n/a
Randy Webb wrote:
to*****@hotpop.com 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?
Sep 28 '06 #3

P: n/a
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...

to*****@hotpop.com 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>
Sep 29 '06 #4

P: n/a
ag******@gmail.com 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/
Sep 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.