<script> include help. Dynamically adding to page and statically adding to page

They have: 426 posts

Joined: Feb 2005

I need some sort of clarification about this really.

I am working on some javascript and something really weired is happening!

I am using JSON with a callback function. I can only get this to work if i actually statically add the javascript to the page rather than dynamically adding it in through the appendChild function (when i dynamically add this script it is definitely there because i can see it through Firefox "view generated code"?)

I also did another experiment with this just to make sure and added all my variables that i have at the top of my main script to an include file and placed it above my main included file. As expected it works, but when i try to insertBefore() the main script and i can see that it has been inserted, the variables are not registered.

This is an example of what i mean:

static version, works fine:

<script src="variables.js"></script>
<script id="idofmaininclude" src="includefile.js"></script>

if i insert the variables.js file by using the below code at the top of the includefile.js file:

var variables = document.createElement("script");
variables.setAttribute("type", "text/javvascript");
variables.setAttribute("src", "sourcetofile.js");
var s = document.getElementById("idofmaininclude");
document.body.insertBefore(variables, s);

It does not work?

If anyone can give me some idea why i would appreciate it.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

check this line

variables.setAttribute("type", "text/javvascript")

should be

variables.setAttribute("type", "text/javascript")

perhaps? Confused

They have: 426 posts

Joined: Feb 2005

OK So to reiterate, i am creating a script element and referencing an external js file, at present the jquery library.

I can see the include script from the generated source, but my error console is telling me that the $ sign is not defined telling me that it cannot see jquery.

It i copy and paste the generated script include and paste it into the head then all is well.

Can someone give me some ideas.........whoever can help me solve this problem will be the webmaster-forums genius!

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I'm not sure if setting the type attribute to "javvascript" [note: 2 vs instead of 1 v] may cause a problem.. does it?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.