/    Sign up×
Community /Pin to ProfileBookmark

element referenced by ID/NAME in global scope??

i get this error when i try to include a script using this: (using firefox)

[code=html]
function includejs(jsFile){
var scriptNode = document.createElement(‘script’);
document.getElementsByTagName(“head”)[0].appendChild(scriptNode);
scriptNode.language=’javascript’;
scriptNode.src=jsFile;
}
<body onload=”includejs(main.js);”>
[/code]

“element referenced by ID/NAME in global scope. Use WC3 standard document.getElementById() instead….

i am soooo confused.. i have tried 100 different variations of this script.

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 12.2006 — Some errors were in your code

  • - you must have quoted the passed variable includejs([COLOR=Red]'[/COLOR]main.js[COLOR=Red]'[/COLOR])

  • - language is deprecated. Use [B]type[/B] instead


  • And use onload rather in the code than within the body tag. In fact onload in that position belongs to the [B]window[/B] object

    try this:
    <i>
    </i>&lt;script type="text/javascript"&gt;
    function includejs(jsFile){
    var scriptNode = document.createElement('script');
    scriptNode.type='text/javascript';
    scriptNode.src=jsFile;
    document.getElementsByTagName('head')[0].appendChild(scriptNode);
    }
    onload=function(){includejs('main.js')}
    &lt;/script&gt;
    Copy linkTweet thisAlerts:
    @KravvitzAug 12.2006 — When you use appendChild() the node is moved from the variable to where you placed it instead of making a reference to it. The appendChild() method returns a reference to the node.

    Also, the language attribute has been deprecated in favor of the type attribute.
    scriptNode = document.getElementsByTagName("head")[0].appendChild(scriptNode);
    scriptNode.type='text/javascript';

    Edit: I repeated some of what Kor said because I didn't see his post until after I had made mine. I strongly agree about not using <body onload="">.
    Copy linkTweet thisAlerts:
    @vwphillipsAug 12.2006 — [CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
    <title></title>
    <script type="text/javascript">
    <!--
    // Check that the file is available & can swap files if required

    var JSAdd;
    var CSSAdd;

    function AddJS(name){
    if (getFile(name)==null){ return; }
    head=document.getElementsByTagName('HEAD')[0];
    // next line removes the previously added External JavaScript
    if (JSAdd){ head.removeChild(JSAdd); }
    JSAdd=document.createElement('SCRIPT');
    JSAdd.type='text/javascript';
    JSAdd.src=name;
    head.appendChild(JSAdd);
    }

    function AddCSS(name){
    if (getFile(name)==null){ return; }
    head=document.getElementsByTagName('HEAD')[0];
    // next line removes the previously added StyleSheet
    if (CSSAdd){ head.removeChild(CSSAdd); }
    CSSAdd=document.createElement('LINK');
    CSSAdd.rel='stylesheet';
    CSSAdd.type='text/css';
    CSSAdd.href=name;
    head.appendChild(CSSAdd);
    }

    function getFile(filename){
    oxmlhttp = null;
    try {
    oxmlhttp = new XMLHttpRequest();
    oxmlhttp.overrideMimeType("text/xml");
    }
    catch(e){
    try { oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch(e){ return null; }
    }
    if(!oxmlhttp) return null;
    try {
    oxmlhttp.open("GET",filename,false);
    oxmlhttp.send(null);
    }
    catch(e){ return null; }
    return oxmlhttp.responseText;
    }


    function Test(){
    alert('An External JavaScriptnhas not been Added');
    }

    //-->
    </script>

    </head>

    <body>
    <input type="button" name="" value="Add JS fred.js" onclick="AddJS('fred.js');" /><br />
    <input type="button" name="" value="Add JS tom.js" onclick="AddJS('tom.js');" /><br />
    <input type="button" name="" value="Test" onclick="Test();" />
    <br />
    <br />
    <input type="button" name="" value="Add CSS ss1.css" onclick="AddCSS('ss1.css');" /><br />
    <input type="button" name="" value="Add CSS ss2.css" onclick="AddCSS('ss2.css');" /><br />
    <span class="color" >Some Test Text</span>
    </body>

    </html>
    [/CODE]
    Copy linkTweet thisAlerts:
    @KravvitzAug 12.2006 — Vic, why are you using an incomplete HTML 4.01 Transitional doctype with code that looks like XHTML 1.0 Transitional?

    I should mention that your code won't work if you served it as application/xhtml+xml instead of text/html because you used uppercase element names in the JS functions (some browsers may also require the use of createElementNS() instead of createElement(), but that's another matter.).
    Copy linkTweet thisAlerts:
    @KorAug 12.2006 — Vic, why are you using an incomplete HTML 4.01 Transitional doctype with code that looks like XHTML 1.0 Transitional?

    I should mention that your code won't work if you served it as application/xhtml+xml instead of text/html because you used uppercase element names in the JS functions (some browsers may also require the use of createElementNS() instead of createElement(), but that's another matter.).[/QUOTE]


    and also CDATA escapes for all the scripts
    Copy linkTweet thisAlerts:
    @dknight3authorAug 12.2006 — hey are you going to help me too? lol
    Copy linkTweet thisAlerts:
    @KorAug 12.2006 — I'll post my code again (it looks like you have not seen it
    <i>
    </i>&lt;script type="text/javascript"&gt;
    function includejs(jsFile){
    var scriptNode = document.createElement('script');
    scriptNode.type='text/javascript';
    scriptNode.src=jsFile;
    document.getElementsByTagName('head')[0].appendChild(scriptNode);
    }
    onload=function(){includejs('main.js')}
    &lt;/script&gt;
    Copy linkTweet thisAlerts:
    @dknight3authorAug 12.2006 — hmmm not working.... www.cleanscripts.com/ajax[/QUOTE]

    do i need to say anything else? jeez....
    Copy linkTweet thisAlerts:
    @KorAug 12.2006 — do i need to say anything else? jeez....[/QUOTE]

    Sir, give this a try (and in your main.js write only the line - alert('it works'):
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Untitled Document&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
    &lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
    &lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
    &lt;script type="text/javascript"&gt;
    function includejs(jsFile){
    var scriptNode = document.createElement('script');
    scriptNode.type='text/javascript';
    scriptNode.src=jsFile;
    document.getElementsByTagName("head")[0].appendChild(scriptNode);
    }
    onload=function(){includejs('main.js')}
    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;

    &lt;/body&gt;
    &lt;/html&gt;

    Now, there must be other errors in your page, I can not see which, as my FF can not even open your page.
    Copy linkTweet thisAlerts:
    @dknight3authorAug 12.2006 — http://www.cleanscripts.com/ajax/asdf.html

    copied it from your post... it works offline but then when i upload it it stops working...
    Copy linkTweet thisAlerts:
    @KorAug 12.2006 — Do you have any other functions triggered by the window.onload ? Or have you left the onlond on your body tag (you must remove it from there)?
    Copy linkTweet thisAlerts:
    @dknight3authorAug 12.2006 — i gave you a link for a reason? jeez... use it maybe?
    Copy linkTweet thisAlerts:
    @KravvitzAug 13.2006 — That's very strange. It works fine locally for me too and it works from my local Apache and IIS servers, but not from your server.
    Copy linkTweet thisAlerts:
    @dknight3authorAug 13.2006 — hmmm.... very strange im going to talk to my host...
    Copy linkTweet thisAlerts:
    @dknight3authorAug 13.2006 — **** i am soooo frustrated with this damn script already... i keep messing around with it and somtimes it works a little but it never totally works...
    ×

    Success!

    Help @dknight3 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.1,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...