如何根据网址中的查询结果提供结果? [英] How provide results based on the query results in a url?
问题描述
是否有一个javascript解决方案来读取url,从中创建一个字符串,并根据结果构建一个if语句?有没有人知道一个教程或可以提供一些如何实现这一目标的技巧。
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");
}
推荐答案
您可以获取网址,或使用 window.location
object。
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
http://www.google.com:80/search?q=devmo#test
这些是 window.location的标准属性
对象,以及上述网址的价值:
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
}
这篇关于如何根据网址中的查询结果提供结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!