首页编程location.search,JS中location.search什么意思

location.search,JS中location.search什么意思

编程之家2023-11-0776次浏览

各位老铁们好,相信很多人对location.search都不是特别的了解,因此呢,今天就来为大家分享下关于location.search以及JS中location.search什么意思的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

location.search,JS中location.search什么意思

location对象中有哪些属性和方法

那么 location对象的属性结果是:

location.hostname= www.baidu.com

location.href= http://www.baidu.com/location.htm?key=asp#result

location.host= www.baidu.com:81

location.hash=#result

location.port= 81

location.search,JS中location.search什么意思

location.pathname=/location.htm

location.search=?key=asp

location.protocol= http:

需要说明

如果 port不为空,则 host= hostname+":"+ port;如果 port为空,则 host= hostname。一般来说我们都使用默认端口 80,所以 port一般为空。

hash是包含#的。

location.search,JS中location.search什么意思

pathname是包含/的。

search是包含?的。

protocol是包含:的。

以上属性均是可读可写的。

如果直接对 location取值赋值,就相当于对 location.href取值赋值。

在 Ajax中,可利用 hash实现前进后退功能。

href= protocol+"//"+ host+ pathname+ search+ hash(在本地双击 htm文件用浏览器打开时可能不适用本公式)

location对象的方法

assign(sURL)读取新的 URL。

reload([bReloadSource]) bReloadSource默认为 false,表示从缓存中重新读取;如果为 true,表示从服务端重新读取。

replace(sURL)读取新的 URL。

需要说明

assign与 replace是有区别的。假设有 assign.htm用 assign跳转到 history.htm,那么在 history.htm的 JavaScript对象 history中就会记录两条历史记录;假设有 replace.htm用 replace跳转到 history.htm,那么在 history.htm的 JavaScript对象 history中就只有一条历史记录(该记录为 history.htm,而不是 replace.htm)。

用 assign跳转和 href跳转则没有什么区别。

javascript的location用法

1.location地址对象描述的是某一个窗口对象所打开的地址。表示当前窗口的地址,只需使用“location”就行;若要表示某一个窗口的地址,就使用“<窗口对象>.location”。具体如下:

第一、location属性、用法以及相关示例:

Location包含了关于当前 URL的信息。location对象描述了与一个给定的 Window对象

关联的完整 URL。location对象的每个属性都描述了 URL的不同特性。

2.属性概览

protocol返回地址的协议,取值为'http:','https:','file:'等等。

hostname返回地址的主机名,例如,一个“

http://www.microsoft.com/china/”的地址,location.hostname==

'www.microsoft.com'。

· port返回地址的端口号,一般 http的端口号是'80'。

· host返回主机名和端口号,如:'www.a.com:8080'。

· pathname返回路径名,如“http://www.a.com/b/c.html”,

location.pathname=='b/c.html'。

· hash返回“#”以及以后的内容,如“

http://www.a.com/b/c.html#chapter4”,location.hash==

'#chapter4';如果地址里没有“#”,则返回空字符串。

· search返回“?”以及以后的内容,如“

http://www.a.com/b/c.asp?selection=3&jumpto=4”,l ocation.search

=='?selection=3&jumpto=4';可以使用

“location.href='...'”,也可以直接用“location='...'”来达

到此目的。

3.方法概览

reload()相当于按浏览器上的“刷新”(IE)或“Reload”(Netscape)

键。

replace()打开一个 URL,并取代历史对象中当前位置的地址。用这个方

法打开一个 URL后,按下浏览器的“后退”键将不能返回到刚才的页面。

location之页面跳转js如下:

//简单跳转

function gotoPage(url){

// eg. var url=

"newsview.html?catalogid="+catalogID+"&pageid="+pageid;

window.location= url;

}

//对location用法的升级,为单个页面传递参数

function goto_catalog(iCat){

if(iCat<=0){

top.location="../index.aspx";// top出去

} else{

window.location="../newsCat.aspx?catid="+iCat;

}

}

对指定框架进行跳转页面,

function goto_iframe(url){

parent.mainFrame.location="../index.aspx";//

// parent.document.getElementById("mainFrame").src=

"../index.aspx";// use dom to change page//同时我增加了dom的写法

}

//对指定框架进行跳转页面,因为

parent.iframename.location="../index.aspx";方法不能实行,主要是

"parent.iframename"中的iframename在js中被默认为节点,而不能把传递过

来的参数转换过来,用dom实现了该传递二个参数的框架跳转页面,

function goto_iframe(iframename,url){

parent.document.getElementById(iframename).src="../index.aspx";//

use dom to change page by iframeName

//}

//回到首页

function gohome(){

top.location="/index.aspx";

JS中location.search什么意思

url中问号后面的一串字符源,一般用来传递数据用的。

Location包含了关于当前 URL的信息。location对象描述了与一个给定的 Window对象关联的完整 URL。location对象的每个属性都描述了URL的不同特性。

类似浏览器地址栏bai地址参数部分格式的正则匹配

var reg= new RegExp("(^|&)"+ name+"=([^&]*)(&|$)","i");

<==> name=abc&pp=cde&...

或者&name=abc&pp=cde&...

匹配这样的类似参数,但是他只dao取name参数部分

name=abc或&name=abc

window.location.search部分是?name=dddcdsdfs的类似

取第一个索引开始就是:name=dddcdsdfs来匹配正则

扩展资料:

使用 start_num可跳过指定数目的字符。

例如,假定使用文本字符串 AYF0093.YoungMensApparel,如果要查找文本字符串中说明部分的第一个 Y的编号,则可将 start_num设置为 8,这样就不会查找文本的序列号部分。

SEARCH将从第 8个字符开始查找,而在下一个字符处即可找到 find_text,于是返回编号 9。SEARCH总是从 within_text的起始处返回字符编号,如果 start_num大于 1,也会对跳过的字符进行计数。

参考资料来源:百度百科-search

window.location.search 相关

A.html,如下,注意需要在问号后面加个参数名p=

<a rel="external nofollow" href="B.html?p=C.html">B</a>

B.html,如下,UrlValue方法里获取的是A.html的a的参数p=后面的值

<script>

window.onload= function(){

var v= document.getElementById("a1");

v.href= v.innerText+".html?p="+ UrlValue("p");

};

function UrlValue(name){

var reg= new RegExp("(^|&)"+ name+"=([^&]*)(&|$)","i");

var r= window.location.search.substr(1).match(reg);

if(r!= null) return unescape(r[2]); return null;

}

</script>

<a id="a1" rel="external nofollow" href="#">C</a>

<script>

window.onload= function(){

var v= document.getElementById("mainFrame");

v.src="C.html?p="+ UrlValue("p");//这里我使用的是C.html,你改一下你要的页面

};

function UrlValue(name){

var reg= new RegExp("(^|&)"+ name+"=([^&]*)(&|$)","i");

var r= window.location.search.substr(1).match(reg);

if(r!= null) return unescape(r[2]); return null;

}

</script>

<frame src="" name="mainFrame" id="mainFrame"/>

扩展资料:

示例

http://www.home.com:8080/windows/location/page.html?ver=1.0&id=timlq#love

1、window.location.href

整个URl字符串(在浏览器中就是完整的地址栏)

返回值:http://www.home.com:8080/windows/location/page.html?ver=1.0&id=timlq#love

2、window.location.protocol

URL的协议部分

返回值:http:

3、window.location.host

URL的主机部分,

返回值:www.home.com

参考资料来源:百度百科-window.location

文章到此结束,希望我们对于location.search的问题能够给您带来一些启发和解决方案。如果您需要更多信息或者有其他问题,请随时联系我们。

javajdk1.6 jdk1.6和jre6的联系与区别增加百度收录(如何增加百度收录)