Javascript警告消息包含URL

Javascript alert message contains URL

本文关键字:URL 包含 消息 警告 Javascript      更新时间:2023-09-26

我正在使用以下javascript函数:

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

我想在警告消息中添加URL后(对不起!),但我不知道如何将URL附加到javascript中的消息本身。

Try -

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

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

您想要使用窗口。Location对象获取当前值。

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

如果你指的是URL:

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

如果你指的是链接,答案是这是不可能的

这将把当前URL放在警告框中。注意,它不是一个可点击的超链接。

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

document.location.hrefdocument.URL,两者都将工作:)

参见http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-46183437