将当前浏览器URL添加到HTML超链接中

Add current browser URL into HTML hyperlink

本文关键字:HTML 超链接 添加 URL 浏览器      更新时间:2023-09-26

我正在制作一个Chrome扩展。

我想将当前URL添加到HTML链接中,如以下示例:

<html>
<body>
  <a href="this-is-where-the-current-link-will-go">Search</a>
</body>
</html>

这怎么可能?

[该代码段将抛出错误,但是窗口。位置是你要找的]

为锚标记设置一个ID。然后这样做:

var link = document.getElementById("demo");
link.href = "https://www.google.com/search?q=" + window.location;
<a id="demo" href="https://www.google.com/search?q=**Current URL**">Search</a>

试试这个

<html>
<body>
<a href="javascript:document.location = 'https://www.google.com/search?q=' + document.URL">Search</a>
</body>
</html>