---恢復內容開始---
1.location.href.....
(1)self.loction.href="http://www.cnblogs.com/url"
window.location.href="http://www.cnblogs.com/url" 以上兩個用法相同均為在當前頁面打開URL頁面
(2)this.location.href="http://www.cnblogs.com/url" 當前頁面打開URL
(3) parent.location.href="http://www.cnblogs.com/url" 在父頁面打開新頁面,如果頁面中自定義了frame,那麼可將parent self top換為自定義frame的名稱,效果是在frame窗口打開url地址
(4) top.location.href="http://www.cnblogs.com/url" 在頂層頁面打開新頁面
2. 關於刷新頁面
(1)window.location.href=http://www.cnblogs.com/nana-share/p/window.location.href
(2)window.location.Reload()
都是刷新當前頁面。區別在於是否有提交數據。當有提交數據時,window.location.Reload()會提示是否提交,window.location.href=http://www.cnblogs.com/nana-share/p/window.location.href;則是向指定的url提交數據
3.
(1)第一段為實際在用的
function getURLParameter(name)
{2 3
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null; //構造一個含有目標參數的正則表達式對象4 5 }
//獲取url中的參數2
function getUrlParam(name)
{3
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構造一個含有目標參數的正則表達式對象4
var r = window.location.search.substr(1).match(reg); //匹配目標參數5
if (r != null) return unescape(r[2]); return null; //返回參數值6
}
例如像獲取下面鏈接的郵箱
http://agent/index.php/Home/Login/getpwd_check_email?code=824790&to=1321136493@qq.com
var mail = getURLParameter('to');
---恢復內容結束---
下面再來看一下js操作url的代碼
代碼很簡單,主要一個思路是把url參數解析為js對象,再做增、刪、改、查操作就很方便了~,這裡做筆記。
var LG=(function(lg){
var objURL=function(url){
this.ourl=url||window.location.href;
this.href="";//?前面部分
this.params={};//url參數對象
this.jing="";//#及後面部分
this.init();
}
//分析url,得到?前面存入this.href,參數解析為this.params對象,#號及後面存入this.jing
objURL.prototype.init=function(){
var str=this.ourl;
var index=str.indexOf("#");
if(index>0){
this.jing=str.substr(index);
str=str.substring(0,index);
}
index=str.indexOf("?");
if(index>0){
this.href=str.substring(0,index);
str=str.substr(index+1);
var parts=str.split("&");
for(var i=0;i<parts.length;i++){
var kv=parts[i].split("=");
this.params[kv[0]]=kv[1];
}
}
else{
this.href=this.ourl;
this.params={};
}
}
//只是修改this.params
objURL.prototype.set=function(key,val){
this.params[key]=val;
}
//只是設置this.params
objURL.prototype.remove=function(key){
this.params[key]=undefined;
}
//根據三部分組成操作後的url
objURL.prototype.url=function(){
var strurl=this.href;
var objps=[];//這裡用數組組織,再做join操作
for(var k in this.params){
if(this.params[k]){
objps.push(k+"="+this.params[k]);
}
}
if(objps.length>0){
strurl+="?"+objps.join("&");
}
if(this.jing.length>0){
strurl+=this.jing;
}
return strurl;
}
//得到參數值
objURL.prototype.get=function(key){
return this.params[key];
}
lg.URL=objURL;
return lg;
}(LG||{}));
LG只是我個人共同JS的名稱空間,無他。調用:
var myurl=new LG.URL("http://www.baidu.com?a=1");
myurl.set("b","hello"); //添加了b=hello
alert (myurl.url());
myurl.remove("b"); //刪除了b
alert(myurl.get ("a"));//取參數a的值,這裡得到1
myurl.set("a",23); //修改a的值為23
alert (myurl.url());