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?
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.
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.
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.
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!');
You can do it like this:
if(document.URL.indexOf("foo_page.html") >= 0){
...show your message
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With