Jump to content

If String Exists In Url Do Something..?


cyfer65
 Share

Recommended Posts

url = window.location;if(url.indexOf("String to search") >= 0) //IndexOf returns the location of the start of the string. If it is greater then or equal to 0, then it must exist.{	 //do something} else {	 //do something else}

Link to comment
Share on other sites

window.location should hold the value of the current address bar, and you can search it for pertinent values a variety of ways. You can see if window.location.indexof('whatever') != -1If that is true, then it contains the string you're looking for and you can redirect as appropriate:if(window.location.indexof('whatever') != -1) {window.location = 'go_here.html';}If your URL is going to be parameterized, you can split the url by '=':var parameter = split('=', window.location)and then paramter[1] will hold the value of the name-value pair (if there is only one). indexof is the easier method and means you don't need to know as much about the structure of the URLs.

Link to comment
Share on other sites

Guys this doesn't seem to be working for me at all..??And when i visit my pages URL --> "http://my-site.com/index.html?google" nothing happens..??Does the code you guys gave me check the entire URL if "google" exists anywhere in it..??And if it does contain google then redirect to 'http://google.com'...??this is the code on my page..Am I missing something here..??

<html><head><script type="text/javascript">if(window.location.indexof('google') != -1) {window.location = 'http://google.com';}</script></head></html>

Link to comment
Share on other sites

When you say not working, does it just do nothing or do yoou get errors? Perhaps you need to store window.location to a variable, in case the problem is that indexOf is not a method of window.location:

var url = window.location;if(url.indexOf('google') != -1) {window.location = "http://google.com";}

Link to comment
Share on other sites

Ah, you figured it out, good.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...