nooby nooby - 1 month ago 21
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.)

<script>
var url="Pastebin.com"
var extra="/74205
</script>

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


Thank you.

Answer

You can give your script tag an id:

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

and then set the src attribute to your desired value:

<script>
  var url="Pastebin.com";
  var extra="/74205";
  document.getElementById('myScript').src = url+extra;
</script>

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