location.href跳轉頁面時傳遞引數並且在新頁面接收引數
阿新 • • 發佈:2020-08-12
原文地址:location.href跳轉頁面時傳遞引數並且在新頁面接收引數
window.location.href = "./punch/clock_frm.html"
問號傳參:
window.location.href = "./punch/clock_frm.html?modFlag="+modFlag+'&role='+role;
那麼我們在新頁面接收引數, 並且將引數轉為可用的json格式時, 可以用下面的方法:
var url = location.search; //獲取url中"?"符後的字串 ('?modFlag=business&role=1')
var theRequest = new Object();
if ( url.indexOf( "?" ) != -1 ) {
var str = url.substr( 1 ); //substr()方法返回從引數值開始到結束的字串;
var strs = str.split( "&" );
for ( var i = 0; i < strs.length; i++ ) {
theRequest[ strs[ i ].split( "=" )[ 0 ] ] = ( strs[ i ].split( "=" )[ 1 ] );
}
console.log( theRequest); //此時的theRequest就是我們需要的引數物件;
console.log( theRequest.modFlag);
console.log( theRequest.role);
}
location.href傳參中文亂碼問題解決
在js中通過window.location.href方式跳轉頁面並在路徑上傳遞引數中文亂碼解決
js中對中文進行編碼:(不對ASCII 字母和數字進行編碼)
window.location.href = ‘aaa.html?Unit=’+encodeURI(encodeURI(中文內容))//有時候需要兩次編碼
window.location.href = ‘aaa.html?Unit=’+encodeURI(中文內容)
在接收頁面接收的時候再解碼回來即可 decodeURI(window.location.href)