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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17268511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:24:16  来源:igfitidea点击:

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

javascripthtmlurl

提问by Hulk1991

In my current window, I need to display the current URL(document.URL) as a link. If I click that link, it will open the same page in a new window.

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

采纳答案by mohkhan

You can do something like this.. Maybe you can improvise on this...

你可以做这样的事情..也许你可以即兴创作......

<a href="#" id="link" target="_blank">Link</a>

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

回答by Benjamin Gruenbaum

No need for JavaScript in order to do this, you can simply use a relative path:

无需 JavaScript 即可执行此操作,您只需使用相对路径即可:

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

This would mean 'link to the current window'

这意味着“链接到当前窗口”

回答by Arun P Johny

You can create a link with id like

您可以创建一个带有 id 的链接

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

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

Then in javascript

然后在javascript中

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