How to provide results based on query results in a url?

Is there a javascript solution to read the url, create a string from it and create an if statement based on the results? Does anyone know a tutorial or can give me some tips on how to do this.

To be more specific, I am trying to do this based on a search result. So, for example, the URL looks something like this:

http://www.site.com/catalogsearch/result?q=asdf&kw=asdf&x=0&y=0

and practicing Daniels answer, I tried this with no luck:

if (window.location.search ==='?q=asdf') { alert("You searched for asdf"); } 
0
javascript
source share
2 answers

You can get the URL or parts for it using the window.location object.

For example, consider the following URL:

http://www.google.com:80/search?q=devmo#test

These are the standard properties of the window.location object and the value you will get for the above URL:

 property | value -----------+----------------------------------------------------- hash | #test host | www.google.com:80 hostname | www.google.com href | http://www.google.com:80/search?q=devmo#test pathname | /search port | 80 protocol | http: search | ?q=devmo 

For example, if you want to check the path name, you can do the following:

 if (window.location.pathname === '/search') { // do something } 
+5
source share

how about this

 var str = window.location.href; if(str.indexOf("http") > - 1){ //ah an if statment! alert("url has http"); } 
0
source share

All Articles