这个javascript代码可以优化吗?
如果当前页面URL的查询字符串中有参数“myid1”或“myid2”,则对于我的网页中具有类“rewrite”的每个链接,我希望将链接href的查询字符串替换为当前页面URL的查询字符串。我正在使用下面给出的代码。由于我是javascript新手,我不确定它是否经过优化。我希望它尽可能快地执行。请帮忙。提前感谢:)这个javascript代码可以优化吗?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,如果当前页面URL的查询字符串中有参数“myid1”或“myid2”,则对于我的网页中具有类“rewrite”的每个链接,我希望将链接href的查询字符串替换为当前页面URL的查询字符串。我正在使用下面给出的代码。由于我是javascript新手,我不确定它是否经过优化。我希望它尽可能快地执行。请帮忙。提前感谢:) $(函数(){ var requestid=gup('myid1'); if(请求ID){ $(“a.rewrite”)。每个(函数(){ var base=this.href;
$(函数(){
var requestid=gup('myid1');
if(请求ID){
$(“a.rewrite”)。每个(函数(){
var base=this.href;
var pos=基准指数(“?”);
如果(位置!=-1){
base=base.substr(0,位置);
}
this.href=base+“?myid1=“+requestid;
})
}
var requestid2=gup('myid2');
如果(请求ID2){
$(“a.rewrite”)。每个(函数(){
var base=this.href;
var pos=基准指数(“?”);
如果(位置!=-1){
base=base.substr(0,位置);
}
this.href=base+“?myid2=“+requestid2;
})
}
})
功能gup(名称)
{
名称=名称。替换(/[\[]/,“\\\[”)。替换(/[\]]/,“\\\]”);
var regexS=“[\\?&]”+name+“=([^&\\]*)”;
var regex=新的RegExp(regexS);
var results=regex.exec(window.location.href);
如果(结果==null)
返回“”;
其他的
返回结果[1];
}
您提供的代码在两方面效率低下:
a中循环。每次为一个查询字符串匹配重写anchor。它可以优化为一个循环
regexS
对每个gup
函数执行,可以将其简化为一次计算window.location.href
数据提取到一个变量中,该变量可以稍后引用可能更适合您的问题。您的意思是,如果您有一个带有查询字符串的窗口url
?myid1=blah
,您想将锚href
的myid1
部分替换为“blah”?感谢Inovation。它起作用了,但我有一个疑问。为什么在if(requestid){
下面还有另一个if条件?为什么不干脆if(requestid){this.href=base+“?myid1=“+requestid;}否则{this.href=base+”?myid2=“+requestid2;}
如果您的两个参数都在url中找到,那么它们都应该被替换。如果答案正确,这将有助于其他用户
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
$(function() {
var requestid = gup('myid1');
if (requestid) {
$("a.rewrite").each(function() {
var base = this.href;
var pos = base.indexOf("?");
if (pos != -1) {
base = base.substr(0, pos);
}
this.href = base + "?myid1=" + requestid;
})
}
var requestid2 = gup('myid2');
if (requestid2) {
$("a.rewrite").each(function() {
var base = this.href;
var pos = base.indexOf("?");
if (pos != -1) {
base = base.substr(0, pos);
}
this.href = base + "?myid2=" + requestid2;
})
}
})
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
</script>
<a href="http://www.website.com/?someid=1234" class="rewrite">Hyperlink</a>
$(function() {
var requestid = gup('myid1');
var requestid2 = gup('myid2');
if (requestid || requestid2) {
$("a.rewrite").each(function() {
var base = this.href;
var pos = base.indexOf("?");
if (pos != -1) {
base = base.substr(0, pos);
}
if (requestid){
this.href = base + "?myid1=" + requestid;
if (requesid2){
this.href += "?myid2=" + requestid;
}
} else {
this.href = base + "?myid2=" + requestid;
}
})
}
});
//First you fetch the query string as key-value pairs in the window.location.href, this equals your gup function.
//This code, fetch the ?key1=value1&key2=value2 pair into an javaScript Object {'key1': 'value1', 'key2':'value2'}
var queryString = {};
var queryStringPattern = new RegExp("([^?=&]+)(=([^&]*))?", "g");
window.location.href.replace(
queryStringPattern,
function($0, $1, $2, $3) { queryString[$1] = $3; }
);
//Second you collect all the anchor with class rewrite and execute the replacement.
$("a.rewrite").each(function () {
this.href.replace(
queryStringPattern,
function ($0, $1, $2, $3) {
return queryString[$1] ? $1 + "=" + queryString[$1] : $1 + '=' + $3;
}
)
});