cyfer65 Posted October 10, 2009 Share Posted October 10, 2009 how can i check the current page url for a string, and if its found in the url then navigate to a different url..?? Link to comment Share on other sites More sharing options...
MrFish Posted October 10, 2009 Share Posted October 10, 2009 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 More sharing options...
chibineku Posted October 10, 2009 Share Posted October 10, 2009 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 More sharing options...
cyfer65 Posted October 11, 2009 Author Share Posted October 11, 2009 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 More sharing options...
Synook Posted October 11, 2009 Share Posted October 11, 2009 http://www.w3schools.com/jsref/jsref_indexOf.aspindexOf() has a capital O. Link to comment Share on other sites More sharing options...
cyfer65 Posted October 11, 2009 Author Share Posted October 11, 2009 is that the only thing wrong with my code..??becuz. I just changed indexOf() and its still not working..?? Link to comment Share on other sites More sharing options...
chibineku Posted October 11, 2009 Share Posted October 11, 2009 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 More sharing options...
cyfer65 Posted October 11, 2009 Author Share Posted October 11, 2009 its still not doing anything. there are no errors.. it just wont redirect to "http://google.com"what can i check to figure out why its not working..??does that code work for you at all..?? Link to comment Share on other sites More sharing options...
cyfer65 Posted October 11, 2009 Author Share Posted October 11, 2009 ok it works now.. it was just needing var url = window.location.href; Link to comment Share on other sites More sharing options...
chibineku Posted October 11, 2009 Share Posted October 11, 2009 Ah, you figured it out, good. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.