最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript alert message contains URL - Stack Overflow

programmeradmin6浏览0评论

I am using the following javascript function:

<!-- 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.

I am using the following javascript function:

<!-- 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.

Share Improve this question edited Sep 20, 2011 at 13:07 Igor 34k14 gold badges82 silver badges116 bronze badges asked Sep 20, 2011 at 12:10 Mohammed AliMohammed Ali 2113 gold badges4 silver badges12 bronze badges 1
  • 1 Did you mean an URL that the user can click on? – robermorales Commented Sep 20, 2011 at 12:14
Add a ment  | 

6 Answers 6

Reset to default 5

Try -

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

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

You want to use the window.location object to get the current value.

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

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.

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

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

document.location.href or document.URL, both will work :)

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

发布评论

评论列表(0)

  1. 暂无评论