Javascript 警报消息包含 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7484915/
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-10-26 00:16:23  来源:igfitidea点击:

Javascript alert message contains URL

javascript

提问by Mohammed Ali

I am using the following javascript function:

我正在使用以下 javascript 函数:

<!-- Notifying message at the beginning of openning the website -->
    <script language="javascript" type="text/javascript">
        alert('Sorry!');
    </script>
    <!--End -->

I want to add the URL after "(Sorry!)" in the alert message but I don't know how to append the URL to the message itself inside the javascript.

我想在警报消息中的“(抱歉!)”之后添加 URL,但我不知道如何将 URL 附加到 javascript 中的消息本身。

回答by ipr101

Try -

尝试 -

alert('Sorry!'+document.URL);

Demo - http://jsfiddle.net/ipr101/Fg3eN/

演示 - http://jsfiddle.net/ipr101/Fg3eN/

回答by epascarello

You want to use the window.locationobject to get the current value.

您想使用window.location对象来获取当前值。

 alert('Sorry! ' + window.location.href);

回答by nikmd23

This will put the current URL in the alert box. Note that it will not be a hyperlink that is clickable.

这会将当前 URL 放入警报框中。请注意,它不会是可点击的超链接。

<script language="javascript" type="text/javascript">
? ? ? ? alert('Sorry! ' + window.location.href);
? ? </script>

回答by Aman Agarwal

document.location.hrefor document.URL, both will work :)

document.location.href或者document.URL,两者都可以工作:)

回答by robermorales

If you mean URL:

如果您的意思是网址:

<script language="javascript" type="text/javascript">
    var url = "http://www.google.es/";
    alert('Sorry!' + url);
</script>

If you mean a link, the answer is that it is not possible.

如果您指的是链接,那么答案是不可能的