将location.href添加到脚本src中

add location.href to a script src

本文关键字:脚本 src 添加 location href      更新时间:2023-09-26

我不是javascript专业人员,我需要实现一件简单的事情。如何将当前浏览器URL添加到脚本src标记中?

最终结果应该是:

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

您可以使用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);

编辑:

如果你可以把脚本附加在头上或身上放

document.head.appendChild(script1); 

document.body.appendChild(script1);

但是,如果你可以输入特定的点(一个div或输入的id),你可以输入:

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

试试这样的东西,对不起,我不在电脑上,所以无法测试

var scr = document.getElementByID('script_name');
<scr onclick="Goto">
function GoTo()
  {
    window.location = 'http://www.google.com'   
  } 
<script id="script_name> </script>