也许你能发现一些我无法发现的东西:
这是我的代码
if(jQuery.urlParam('returnview')) {
var ret = jQuery.urlParam('returnview');
var iid = jQuery.urlParam('iid');
window.location = 'index.php?option=mycomponent&view='+ret+'&Itemid='+iid+'&lang=en';
} else if(!jQuery.urlParam('returnview')){
window.location = 'index.php?option=mycomponent&view=myview&Itemid=380&lang=en&sent=1';
} else {
alert('something is dodge');
}
这是函数:
jQuery.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
现在,如果我的“from”URL 中定义了“returnview”,则它可以正常工作。 但是,如果没有定义 returnview,则应该转到第二种情况,或者即使失败,也会引发警报。
有人能看出我在这里做错了什么吗?
谢谢
雅克
最佳答案
你的第三个条件永远不会被命中,因为你正在测试真/假,所以让我们删除它,留下:
if(jQuery.urlParam('returnview')) {
var ret = jQuery.urlParam('returnview');
var iid = jQuery.urlParam('iid');
window.location = 'index.php?option=mycomponent&view='+ret+'&Itemid='+iid+'&lang=en';
} else{
window.location = 'index.php?option=mycomponent&view=myview&Itemid=380&lang=en&sent=1';
}
然后让我们将变量移到 if 之外并专门检查 false(如果返回的值等于 false,则需要更新我们在下面执行的原始函数):
var ret = jQuery.urlParam('returnview');
var iid;
if(ret === false) {
window.location = 'index.php?option=mycomponent&view=myview&Itemid=380&lang=en&sent=1';
} else{
iid = jQuery.urlParam('iid');
window.location = 'index.php?option=mycomponent&view='+ret+'&Itemid='+iid+'&lang=en';
}
...最后让我们修复原来的函数:
jQuery.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return (results && results[0] ? results[0] : false);
}
我还没有测试过,但我认为应该可以修复它
关于JavaScript URL重定向: Is there an issue with this code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15496097/