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>
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);
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.