Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the src= property of the <script> tag

Status
Not open for further replies.

JLONON

Programmer
Mar 10, 2003
6
US
I have the following:

<script id=&quot;myID&quot; language=&quot;JavaScript&quot;>

I want this:

<script id=&quot;myID&quot; language=&quot;JavaScript&quot; src=&quot;myJSFile.js&quot;>

and I want the &quot;myJSFile.js&quot; portion of the src= property inside this tag to come from a var for example the var srcfile=&quot;myJSFile.js&quot; has been set in some preceding function and is in scope.

So it would look like this?

<script id=&quot;myID&quot; language=&quot;JavaScript&quot; src= srcfile >

How do I tell the script tag to use this var? I think it would be by a DOM method, and it seems that there should be a way to do this, but I don't know how. Thanks!

Jonathan.
 
>How do I tell the script tag to use this var?

Store it in an ASP variable (assuming you're using ASP)...
Code:
<script id=&quot;myID&quot; language=&quot;JavaScript&quot; src=&quot;<%=srcfile%>&quot; >


Good Luck! [thumbsup2]
WindUp
 
Actually, it's simply JavaScript, not ASP. Although JSP is being used too, the code above is in an HTML file so that the combination is HTML/JavaScript only. Any other suggestions or others with thoughts on this?

Besides, will this work with JSP? In other words, if I do have the same srcfile value (or var) in a JSP, can I use it like this: src=&quot;<%=srcfile%>&quot; in my JavaScript script tag inside the HTML?? I will try the latter, but hope an answer might be forthcoming too. Thanks for the reply!

Jonathan.
 
Jonathan,

like so:

<script id=&quot;myID&quot; language=&quot;JavaScript&quot;></script>

<script language=&quot;JavaScript&quot;>
var srcfile = &quot;myJSFile.js&quot;;
document.getElementById(&quot;myID&quot;).src = srcfile;
</script>


=========================================================
while (!succeed) try();
-jeff
 
Thanks Jeff. It worked fine! :) Thanks for all replies. I'll be sure to donate something too.

L8r,
Jonathan.
 
I have noticed a slight side-effect.

Can someone help?

This statement inside one of my JavaScript start/end tag sets:

document.getElementById(&quot;myID&quot;).src = srcfile;

Causes this error the first time the page is opened with a new srcfile value:

Internet Explorer cannot open the Internet site <Web address here>. Operation aborted. <ok>

Every time after the first, this does not happen, as if a cached indicator of some kind knows that the page really is ok!

I think it’s the DOM object and something I don’t know about it. I am using IE5.5. Any ideas welcome.

Thanks,
Jonathan.
 
odd...i haven't used this technique of modifying src property of a <script> tag so i'm only guessing, but try this: wrap whatever code that depends on the script include in a function that uses try/catch to load the include:



function loadIt() {
try {
document.getElementById(&quot;myID&quot;).src = srcfile;
// success: do other stuff here
} catch(E) {
// error: try again
window.setTimeout(&quot;loadIt();&quot;, 50);
}
}



=========================================================
while (!succeed) try();
-jeff
 
if that doesnt fix it, try setting the source to '' before you modify it in the javascript.

Code:
<script id=&quot;myID&quot; language=&quot;JavaScript&quot; src=&quot;&quot;></script>

&quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
 
I haven't had any success with setting the .src property of a <script> tag on a mac in either ie or netscape. Has anyone had any luck with this?

Thanks in advance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top