...

View Full Version : Javascript variable as value for src??



Disco_Cowboy
01-05-2011, 09:46 PM
Is it possible to pass the value of a javascript variable through src? For example, instead of...

<script type="text/javascript" src="/myfolder/myjsfile.js"></script>

...could I have something like...

<script type="text/javascript" src=JS VARIABLE VALUE></script>

What would be the syntax for this? Is it even possible?

Thank you!

gizmo1650
01-05-2011, 10:00 PM
<script type="text/javascript" src="about:blank" id="js">

<script type="text/javascript">
foo=www.test.com/foo.js
document.getElementById('js').src=foo;

Disco_Cowboy
01-06-2011, 03:22 PM
I'm having some trouble getting that to work. Here's how I was trying to put it together...


<script language="text/javascript">
if (location.hostname == "stage.mydomain.com")
var wa_path="//stage.mydomain.com/scripts/blabla.js"
else
var wa_path="//www.mydomain.com/scripts/blabla.js"
document.getElementById('js').src=wa_path;</script>
<script type="text/javascript" src="about:blank" id="js"></script>


Where am I going wrong?

devnull69
01-06-2011, 04:31 PM
<script type="text/javascript" src="about:blank" id="js">

<script type="text/javascript">
foo=www.test.com/foo.js
document.getElementById('js').src=foo;


You should ignore that completely. This will not produce anything useful.

The image


<img src="dummy.jpg" id="myImage"/>

The javascript


var mySrc = 'http://www.mydomain.com/image.jpg';
document.getElementById('myImage').src = mySrc;

Disco_Cowboy
01-06-2011, 04:55 PM
But there is no image involved. I am basically trying to set the path to my other .js file in a somewhat dynamic way based on the current host name. Normally a relative path would obviously be the simplest way to do this but that presents problems on certain CMS pages so I was hoping to dynamically "hard code" the full path based on the host name.

Perhaps I need a different approach??

DaveyErwin
01-06-2011, 05:25 PM
<HTML>
<HEAD>
<TITLE>&nbsp;</TITLE>
<script type="text/javascript">
if (location.hostname == "stage.mydomain.com")
var wa_path="js1.js"
else
var wa_path="js2.js"
document.write('<script type="text/javascript" src="'+wa_path+'"></sc')
document.write('ript')
</script>
</HEAD>
<BODY>&nbsp;
</BODY>
</HTML>

Dormilich
01-06-2011, 05:30 PM
you can make a subdomain referencing your Javascript directory and use that as address. this also loads faster, btw.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum