PDA

View Full Version : include javascript inside javascript



scorgit
May 5th, 2003, 02:33 PM
Hi all,

little (I think a dumb) problem...
I have a javascript file for a menu. Inside it, I want to take up some code to include another javascript file, depending on the accesslevel a logged in user has.

So, an admin will see the whole menu, a simple mortal will see less.

What is the code to include a javascript file into another javascript file?

Thanx!

Tom

requestcode
May 5th, 2003, 02:49 PM
You can't include a JavaScript file within another. The code will have to be included separately.

scorgit
May 5th, 2003, 03:51 PM
ok, that's all I wanted to know, so now I can move on with my live. Thnx!

kansel
May 5th, 2003, 10:31 PM
You can try this.

<script>
if(accesscode == "admin")
document.write("<script src=\"menu_admin.js\"><\/script>");
else
document.write("<script src=\"menu_plain.js\"><\/script>");
</script>
The <\/script> has the \ character so some browsers won't think it's the closing tag for the container script tag.

Of course you'll want to set language and type for all those script tags.

russ801
May 6th, 2003, 07:52 PM
Can you include multiple external scripts files?

So that admin would not only have a admin js file but also the base user file.

Vladdy
May 6th, 2003, 08:04 PM
if(somecondition)
{ myscript = document.createElement('script');
myscript.type = 'text/javasript';
myscript.src = 'myscript.js';
document.getElementsByTagName('head')[0].appendChild(myscript);
}