Reputation: 1709
Perhaps you can spot something I cannot:
This is my code
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');
}
and this is the function:
jQuery.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
Now, if there is a 'returnview' defined in my 'from' URL, it works fine. However, if there is no returnview defined, it should go to the second case or even if that fails, throw an alert.
Can anyone see anything obvious I am doing wrong here?
Thanks
Jacques
Upvotes: 0
Views: 157
Reputation: 18233
Simply set the url parameters conditionally based on whether your function returns a truthy value:
var ret = jQuery.urlParam('returnview');
var iid = jQuery.urlParam('iid');
var view = ret || "myview";
var id = iid || "380";
window.location = 'index.php?option=mycomponent&view='+
view + '&Itemid=' + id + '&lang=en';
And for your urlParam
function, you need to make sure you're only capturing the parameter itself, not the entire "&abc=xyz"
segment. You just add parentheses around the desired portion to capture that, then take the second match (the first index). Before dereferencing the match array, check that it isn't null:
jQuery.urlParam = function(name){
var re = RegExp('[\\?&]' + name + '=([^&#]*)');
var results = window.location.href.match(re);
return results ? results[1] : null;
}
Upvotes: 0
Reputation: 5879
Your third conditional will never get hit, because you're testing for true / false so lets remove that, leaving you with:
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';
}
Then lets move the vars outside of the ifs and check for false specifically (in case the value returned is equal to false, this needs an update to your original function which we do below):
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';
}
...and finally let's fix your original function:
jQuery.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return (results && results[0] ? results[0] : false);
}
I haven't tested it but I think that should fix it
Upvotes: 2
Reputation: 1563
check what is under jQuery.urlParam('returnview')
im pretty sure that if value is not set you will get undefined
.
Try jQuery.urlParam('returnview') === undefined
Also check out this post: Get escaped URL parameter
Upvotes: 1