Jump to content

If String Exists In Url Do Something..?


cyfer65

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...