愚蠢的问题,但我想创建或设置一个图像 src,在字符串后面附加当前 url。
所以我想写
<img src="//testURL.comaction?item={www.currenturl.com}" id="tabUrl" />
因此 {www.currenturl.com} 将替换为当前 URL
我知道我可以通过 using window.location.href;
获取当前 URL;但是如何将其添加到字符串中?
不确定是否应该创建一个变量并使用 document.getElementById 应用它
taboo = window.location.href;
或者编写类似于下面示例的文档
<script>document.write('<img src="'//testURL.comaction?item= +
window.location.href + '">')</script>
感谢任何帮助。
最佳答案
你的报价有点偏差:试试这个
'<img src=//testURL.comaction?item=' + window.location.href + '>'
关于javascript - 如何仅使用 Javascript 将当前 URL 字符串附加到图像 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43478220/