Question

I'm not a javascript pro, and I need to achieve a simple thing. How can I add the current browser URL to a script src tag?

The end result should be:

<script src="http://example.com/script.js?ref= {location.href} "></script>
Was it helpful?

Solution

You can use the createElement:

         var link = "http://example.com/script.js?ref="+location.href;
         script1 = document.createElement('SCRIPT');
         script1.type="text/javascript";
         script1.src = src1;
           document.head.appendChild(script1);

EDIT:

If you can append the script in the head or body putting

document.head.appendChild(script1); 

or

document.body.appendChild(script1);

But if you can put in the specific point (id of one div or input), you can put with:

document.getElementById("NAMEOFID).appendChild(script1);

OTHER TIPS

Try something like this, sorry im not at computer so can't test

var scr = document.getElementByID('script_name');

<scr onclick="Goto">

function GoTo()
  {
    window.location = 'http://www.google.com'   
  } 
<script id="script_name> </script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top