javascript - 如何仅使用 Javascript 将当前 URL 字符串附加到图像 URL

标签 javascript

愚蠢的问题,但我想创建或设置一个图像 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/

相关文章:

javascript - 使用 CloudMQTT 进行 Node JS 实时文本更新

javascript - 使用 django 将值传递给引导模式形式

javascript - 如何通过提示在输入字段中输出链接?

javascript - react JS : Check for comma within input value

javascript - 验证表单 JavaScript

javascript - 为什么没有为 document.readyState== ="complete"加载 javascript

Javascript:围绕特定数字对整数数组进行排序

javascript - 严格所有插件特定规则

javascript - laravel ajax验证失败怎么办?

javascript - JQuery:将新单元格附加到表格并循环遍历它们在 FF、Chrome 中有效,但在 IE 中无效