Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If the URL contains this do that in Javascript

Tags:

javascript

I want to display a certain message on a certain page.

Suppose the name of the page I want to display something on is called "foo_page.html",

How can I do this using javascript?

like image 424
Sam Avatar asked May 17 '10 15:05

Sam


People also ask

How do I check if a URL contains a string?

Use indexOf() to Check if URL Contains a String When a URL contains a string, you can check for the string's existence using the indexOf method from String. prototype. indexOf() . Therefore, the argument of indexOf should be your search string.

What does URL indexOf do?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Does URL contain?

The URL contains the name of the protocol needed to access a resource, as well as a resource name. The first part of a URL identifies what protocol to use as the primary access medium. The second part identifies the IP address or domain name -- and possibly subdomain -- where the resource is located.


2 Answers

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!');
like image 23
zaf Avatar answered Sep 20 '22 17:09

zaf


You can do it like this:

if(document.URL.indexOf("foo_page.html") >= 0){ 
...show your message
}
like image 50
GShenanigan Avatar answered Sep 19 '22 17:09

GShenanigan