moving script from body to head

T

toby989

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>
 
R

Randy Webb

(e-mail address removed) 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?
 
T

toby989

Randy said:
(e-mail address removed) said the following on 9/28/2006 1:45 PM:



Yes, that is precisely what it does.



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(fragment)
}
</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.
 
A

agapeton

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...
 
R

Randy Webb

(e-mail address removed) 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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top