If the URL contains this do that in Javascript

You can use document.location to figure out what URL the visitor is at.

Try this:

<script type="text/javascript">
var currentPage = document.location.href.substring(document.location.href.lastIndexOf("/")+1, document.location.href.length);
</script>

Your "currentPage" variable should now contain the name of the page you're on. You can use that to select an action.


var index = document.location.lastIndexOf("/");
var filename = document.location.substr(index);

if(filename.indexOf("foo_page.html")>-1){
   alert("OK");
}

You can do it like this:

if(document.URL.indexOf("foo_page.html") >= 0){ 
...show your message
}

The following will show an alert box if the url is something like http://example.com/foo_page.html :

if(location.pathname=="/foo_page.html") alert('hey!');

Tags:

Javascript