JavaScript-如何在当前窗口中将当前URL显示为链接

JavaScript - How to display the current URL as a link in current window?

本文关键字:URL 显示 链接 窗口 JavaScript-      更新时间:2023-09-26

在当前窗口中,我需要将当前URL(document.URL)显示为链接。如果我点击该链接,它将在一个新窗口中打开同一页面。

您可以这样做。。也许你可以即兴发挥。。。

<a href="#" id="link" target="_blank">Link</a>
<script type="text/javascript">
window.onload = function(){
    document.getElementById("link").href = window.location.toString();
}
</script>

不需要JavaScript,只需使用一个相对路径:

<a href="./" target="_blank">Hello</a>

这意味着"链接到当前窗口"

您可以创建一个id类似的链接

<a id="page-url" href="#">URL</a>

然后在javascript 中

document.getElementById('page-url').href = document.URL