View Full Version : How to use a variable for the SRC in <script> tag?
PhotoJoe47
10-25-2005, 01:48 AM
I have now figure out how to pass the name of a js.file from one page to another. But I can't seem to get the syntax correct for putting the variable into the following line of code to call the *.js file.
<script type="javascript" src="filename"></script>
Now where the filename goes I have tried putting the name of the varialbe with and without the quotes like this
<script type = "javascript">
var File1 ="jsfile1.js"
</script>
<script type="javascript" src=File1></script>
<script type="javascript" src="File1"></script>
I have also tried to put the quotes into the variable like this:
<script type = "javascript">
var File1 ="\'jsfile1.js\'"
</script>
So how do you get a string variable into the opening <script> tag?
PhotoJoe
diablo2_v
10-25-2005, 02:40 AM
You could try something like this:
<script type = "javascript">
var File1 ="jsfile1.js"
document.write("<script type='javascript' src='" + File1 + "></script>";
</script>
PhotoJoe47
10-25-2005, 03:44 AM
You could try something like this:
<script type = "javascript">
var File1 ="jsfile1.js"
document.write("<script type='javascript' src='" + File1 + "></script>";
</script>
Thanks "diablo2_v"
I have have already tried that, and it does not work.
When I look at the line with the document.write in FrontPage the "</script>" is not in the color I use for javascript(brown). It show up in the color use for html tags When the test page is view with my browser all I see the "; that is at the very end of the line and I get error code.
I have played with the document write for hours today trying to get that same type of document.write to work, but never could.
If you were to add this code to the above:
var tempstring = "<>";
document.write(tempstring);
you would not see anything on the screen. But if you have this:
var tempstring = "\x3c \x3e";
The screen would display a pair of <>
Even when I use the hex codes for the <>, I still can't get the js file to load.
PhotoJoe
diablo2_v
10-25-2005, 04:01 AM
Just leave out the end script. It worked for me.
<script type = "javascript">
var File1 ="jsfile1.js"
document.write("<script type='javascript' src='" + File1 + ">";
</script>
Hi PhotoJoe
Take a close look at my post in this thread
http://www.codingforums.com/showthread.php?t=70759
note that the forward slash in the closing tag is escaped with a back slash. i.e.
<\/script>
This is so the browser won't see the tag as closing the parent script
Also, don't trust FP for syntax highlighting.
Well I guess I should have answered your question :rolleyes:
document.write('<script type="text/javascript" src="'+File1+'"><\/script>')
PhotoJoe47
10-25-2005, 05:15 AM
Hi PhotoJoe
Take a close look at my post in this thread
http://www.codingforums.com/showthread.php?t=70759
note that the forward slash in the closing tag is escaped with a back slash. i.e.
<\/script>
This is so the browser won't see the tag as closing the parent script
Also, don't trust FP for syntax highlighting.
I missed the back slash on your previous post. I have been using the document.write to call the second js file as the last comand in the first js file and it has been working, why I don't know?
Thanks again to all of you
PhotoJoe
PhotoJoe47
10-25-2005, 06:23 AM
Ok you guys ( I like "you all" better),
Here is the line of code that gets page working the way I want.
<script type="text/javascript">
document.write("<script type='text/javascript' src="+ document.location.search.substring(1)+ "><\/script>");
</script>
The document.location.search gets the string of the my js file that is passed by the previous page. The .substring(1) gets rid of the "?"
So now I'm a happy camper for now.
PhotoJoe
a DOM solution might be more eficient
<head>
...
...
<script type="text/javascript">
onload = function(){
var jsloc=location.href.split('?')[location.href.split('?').length-1]
var root=document.getElementsByTagName('head')[0];
var oS = document.createElement('script');
oS.setAttribute('type','text/javascript');
oS.setAttribute('src',jsloc);
root.appendChild(oS)
}
</script>
</head>
PhotoJoe47
10-25-2005, 05:33 PM
Thanks Kor,
I guess I could plug that in and it would work. But I do not know anything about DOM (yet). So I do not understand how it works. If you have the time could you explain the different part and what it is doing?
PhotoJoe
<script type="text/javascript">
onload = function(){//function is called onload
var jsloc=location.href.split('?')[location.href.split('?').length-1];//the passed value
var root=document.getElementsByTagName('head')[0];// the root (the HEAD)
var oS = document.createElement('script');//creates the element tag <script>
oS.setAttribute('type','text/javascript');//creates the attribute type, assigns the value 'text/javascript
oS.setAttribute('src',jsloc);//creates the attribute src, assigns the value jsloc
root.appendChild(oS);// appends the element script to the root
}
</script>
PhotoJoe47
10-26-2005, 09:43 PM
<script type="text/javascript">
onload = function(){//function is called onload
var jsloc=location.href.split('?')[location.href.split('?').length-1];//the passed value
var root=document.getElementsByTagName('head')[0];// the root (the HEAD)
var oS = document.createElement('script');//creates the element tag <script>
oS.setAttribute('type','text/javascript');//creates the attribute type, assigns the value 'text/javascript
oS.setAttribute('src',jsloc);//creates the attribute src, assigns the value jsloc
root.appendChild(oS);// appends the element script to the root
}
</script>
Thanks Kor,
That helps.
But I have a question.
Would the following line of code give the same results
var jsloc=location.search.substring(1);
as this line of code?
var jsloc=location.href.split('?')[location.href.split('?').length-1];
PhotoJoe
glenngv
10-27-2005, 08:25 AM
Using location.search is simpler and better. You just have to add checking if nothing is passed in the url.
onload = function(){
var jsloc=location.search.substring(1);
if (jsloc != ""){
var root=document.getElementsByTagName('head')[0];
var oS = document.createElement('script');
oS.setAttribute('type','text/javascript');
oS.setAttribute('src',jsloc);
root.appendChild(oS);
}
else {
alert("Path to the script is not passed. Other scripts that depend on this will not work.");
}
}
VortexCortex
10-27-2005, 08:05 PM
Just incase you were wondering why the first solution didn't work:
you can't use the string "</script>" inside of your <script> tag without ending it prematurely... so you'll need to break it up like this.
<script type="text/javascript"><!--
var filename="myscript.js";
document.write('<script type="text/javascript" src="' + filename + '"></scr'+'ipt>');//two part script end tag.
//--></script>
felgall
10-27-2005, 10:52 PM
you can't use the string "</script>" inside of your <script> tag without ending it prematurely... so you'll need to break it up
No you just need to escape it like this:
document.write('<\/script>');
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.