moving script from body to head

Discussion in 'Javascript' started by toby989@hotpop.com, Sep 28, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Randy Webb Guest

    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/
     
    Randy Webb, Sep 28, 2006
    #2
    1. Advertising

  3. Guest

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


    >
    >
    >>
    >>
    >> <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, 2006
    #3
  4. Guest

    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>
     
    , Sep 29, 2006
    #4
  5. Randy Webb Guest

    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/
     
    Randy Webb, Sep 29, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    821
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    782
    Sherif ElMetainy
    Jul 10, 2004
  3. Karl
    Replies:
    0
    Views:
    433
  4. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    262
    Sam Fields
    Jan 16, 2004
  5. Water Cooler v2
    Replies:
    2
    Views:
    506
    Water Cooler v2
    Apr 18, 2006
Loading...

Share This Page