用javascript替換URL中的參數(shù)值示例代碼
更新時間:2014年01月27日 10:18:19 作者:
本篇文章主要是對用javascript替換URL中的參數(shù)值示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
今天遇到一個需要用javascript將url中的某些參數(shù)替換的需求,想起了不久前從網(wǎng)上淘到了一個parseUrl函數(shù),正好可以借此實現(xiàn),代碼整理如下:
//分析url
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':', ''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function () {
var ret = {},
seg = a.search.replace(/^\?/, '').split('&'),
len = seg.length, i = 0, s;
for (; i < len; i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
hash: a.hash.replace('#', ''),
path: a.pathname.replace(/^([^\/])/, '/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
segments: a.pathname.replace(/^\//, '').split('/')
};
}
//替換myUrl中的同名參數(shù)值
function replaceUrlParams(myUrl, newParams) {
/*
for (var x in myUrl.params) {
for (var y in newParams) {
if (x.toLowerCase() == y.toLowerCase()) {
myUrl.params[x] = newParams[y];
}
}
}
*/
for (var x in newParams) {
var hasInMyUrlParams = false;
for (var y in myUrl.params) {
if (x.toLowerCase() == y.toLowerCase()) {
myUrl.params[y] = newParams[x];
hasInMyUrlParams = true;
break;
}
}
//原來沒有的參數(shù)則追加
if (!hasInMyUrlParams) {
myUrl.params[x] = newParams[x];
}
}
var _result = myUrl.protocol + "://" + myUrl.host + ":" + myUrl.port + myUrl.path + "?";
for (var p in myUrl.params) {
_result += (p + "=" + myUrl.params[p] + "&");
}
if (_result.substr(_result.length - 1) == "&") {
_result = _result.substr(0, _result.length - 1);
}
if (myUrl.hash != "") {
_result += "#" + myUrl.hash;
}
return _result;
}
//輔助輸出
function w(str) {
document.write(str + "<BR>");
}
var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
w("myUrl.file = " + myURL.file) // = 'index.html'
w("myUrl.hash = " + myURL.hash) // = 'top'
w("myUrl.host = " + myURL.host) // = 'abc.com'
w("myUrl.query = " + myURL.query) // = '?id=255&m=hello'
w("myUrl.params = " + myURL.params) // = Object = { id: 255, m: hello }
w("myUrl.path = " + myURL.path) // = '/dir/index.html'
w("myUrl.segments = " + myURL.segments) // = Array = ['dir', 'index.html']
w("myUrl.port = " + myURL.port) // = '8080'
w("myUrl.protocol = " + myURL.protocol) // = 'http'
w("myUrl.source = " + myURL.source) // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
var _newUrl = replaceUrlParams(myURL, { id: 101, m: "World", page: 1,"page":2 });
w("<BR>新url為:")
w(_newUrl); //http://abc.com:8080/dir/index.html?id=101&m=World&page=2#top
復(fù)制代碼 代碼如下:
//分析url
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':', ''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function () {
var ret = {},
seg = a.search.replace(/^\?/, '').split('&'),
len = seg.length, i = 0, s;
for (; i < len; i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
hash: a.hash.replace('#', ''),
path: a.pathname.replace(/^([^\/])/, '/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
segments: a.pathname.replace(/^\//, '').split('/')
};
}
//替換myUrl中的同名參數(shù)值
function replaceUrlParams(myUrl, newParams) {
/*
for (var x in myUrl.params) {
for (var y in newParams) {
if (x.toLowerCase() == y.toLowerCase()) {
myUrl.params[x] = newParams[y];
}
}
}
*/
for (var x in newParams) {
var hasInMyUrlParams = false;
for (var y in myUrl.params) {
if (x.toLowerCase() == y.toLowerCase()) {
myUrl.params[y] = newParams[x];
hasInMyUrlParams = true;
break;
}
}
//原來沒有的參數(shù)則追加
if (!hasInMyUrlParams) {
myUrl.params[x] = newParams[x];
}
}
var _result = myUrl.protocol + "://" + myUrl.host + ":" + myUrl.port + myUrl.path + "?";
for (var p in myUrl.params) {
_result += (p + "=" + myUrl.params[p] + "&");
}
if (_result.substr(_result.length - 1) == "&") {
_result = _result.substr(0, _result.length - 1);
}
if (myUrl.hash != "") {
_result += "#" + myUrl.hash;
}
return _result;
}
//輔助輸出
function w(str) {
document.write(str + "<BR>");
}
var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
w("myUrl.file = " + myURL.file) // = 'index.html'
w("myUrl.hash = " + myURL.hash) // = 'top'
w("myUrl.host = " + myURL.host) // = 'abc.com'
w("myUrl.query = " + myURL.query) // = '?id=255&m=hello'
w("myUrl.params = " + myURL.params) // = Object = { id: 255, m: hello }
w("myUrl.path = " + myURL.path) // = '/dir/index.html'
w("myUrl.segments = " + myURL.segments) // = Array = ['dir', 'index.html']
w("myUrl.port = " + myURL.port) // = '8080'
w("myUrl.protocol = " + myURL.protocol) // = 'http'
w("myUrl.source = " + myURL.source) // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
var _newUrl = replaceUrlParams(myURL, { id: 101, m: "World", page: 1,"page":2 });
w("<BR>新url為:")
w(_newUrl); //http://abc.com:8080/dir/index.html?id=101&m=World&page=2#top
相關(guān)文章
如何基于小程序?qū)崿F(xiàn)發(fā)送語音消息及轉(zhuǎn)文字功能
最近為小程序增加語音識別轉(zhuǎn)文字的功能,坑路不斷,特此記錄,下面這篇文章主要給大家介紹了關(guān)于如何基于小程序?qū)崿F(xiàn)發(fā)送語音消息及轉(zhuǎn)文字功能的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11解決Layui選擇全部,換頁checkbox復(fù)選框重新勾選的問題方法
今天小編就為大家分享一篇解決Layui選擇全部,換頁checkbox復(fù)選框重新勾選的問題方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08深入理解JavaScript函數(shù)參數(shù)(推薦)
這篇文章主要介紹了深入理解JavaScript函數(shù)參數(shù)(推薦)的相關(guān)資料,需要的朋友可以參考下2016-07-07javascript 使用sleep函數(shù)的常見方法詳解
這篇文章主要介紹了javascript 使用sleep函數(shù)的常見方法,結(jié)合實例形式分析總結(jié)了javascript sleep函數(shù)的功能、常見使用方法與操作注意事項,需要的朋友可以參考下2020-04-04