nooby nooby - 3 years ago 124
Javascript Question

Javascript using variable in src

is it possible to do something like this,
(really new to js, just wondering if something like this is possbile couldn't find anything on the internet.)

var url=""
var extra="/74205

<script src=url+extra></script>

Thank you.

Answer Source

You can give your script tag an id:

<script id="myScript"></script>

and then set the src attribute to your desired value:

  var url="";
  var extra="/74205";
  document.getElementById('myScript').src = url+extra;

the script tag with id (myScript) should appear first in page in order for document.getElementById to work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download