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
6 Answers
Reset to default 5Try -
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