Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript variable as value for src??

    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!

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    Code:
    <script type="text/javascript" src="about:blank" id="js">
    Code:
    <script type="text/javascript">
    foo=www.test.com/foo.js
    document.getElementById('js').src=foo;

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm having some trouble getting that to work. Here's how I was trying to put it together...

    Code:
    	<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?

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Quote Originally Posted by gizmo1650 View Post
    Code:
    <script type="text/javascript" src="about:blank" id="js">
    Code:
    <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
    Code:
    <img src="dummy.jpg" id="myImage"/>
    The javascript
    Code:
    var mySrc = 'http://www.mydomain.com/image.jpg';
    document.getElementById('myImage').src = mySrc;

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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??

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
    <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>

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    you can make a subdomain referencing your Javascript directory and use that as address. this also loads faster, btw.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •