Is there a javascript solution to read the url,create a string from it, and build a if statement based on the results? Does anyone know of a tutorial or can provide me with some tips of how to accomplish this.
To be more specific I am trying to do this开发者_如何转开发 based on a search result. So.. for example the url is something like:
http://www.site.com/catalogsearch/result?q=asdf&kw=asdf&x=0&y=0
and working off of Daniels response I am trying this with no luck:
if (window.location.search ==='?q=asdf') {
alert("You searched for asdf");
}
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 would 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 pathname, you could do the following:
if (window.location.pathname === '/search') {
// do something
}
how about this
var str = window.location.href;
if(str.indexOf("http") > - 1){
//ah an if statment!
alert("url has http");
}
精彩评论