开发者

How do i reload same page using window.location in jQuery?

I have a JQuery Function

(function ($) {
jQuery.fn.saveUser = function(destinationUrl) {
    this.click(function() {
        var formData = 'option=saveuser&'+$('form#save-user').serialize();
        $.ajax({
            type: 'POST',
            url:  'process.php',
            data: formData,
            success: function(msg){
                if(msg === 'empty') {
                    alert('Required Values Missing');
                } else if(msg === 'duplicateEmail'){
                    alert('Email already exist');
                } else {
                    window.location = destinationUrl;
                }
            }
        });
    });
}

now based on some actions and events, i redirect it to different pages, in this case i need it to redirect it to two different url.

1) reload the same page preserving all $_GET variables via URI

2) reload to the specified url.

the below jQuery code can redirect me to any specific url.

$('form#save-user button[name="save-user-close"]').saveUser(function() {
    return 'index.php?users';
});

however i am not getting how do i make it redirect to the same page for example.

$('form#save-user button[name="save-user-close"]').saveUser(function() {
    return location.reload(true);
});

i know the above code will not work, i am confused on how do i go wi开发者_StackOverflowth this?


You are never calling the function in your code, so it would not work whatever you put in the function. Intead of:

window.location = destinationUrl;

you should call the function, and let the function do the redirection:

destinationUrl();

(And you probably want to rename the parameter to reflect the change of usage.)

So your functions should do the redirection instead of returning the URL, so that you can use the reload method in the second one:

$('form#save-user button[name="save-user-close"]').saveUser(function() {
  window.location = 'index.php?users';
});

and:

$('form#save-user button[name="save-user-close"]').saveUser(function(e) {
  location.reload(true);
});


window.location.href=window.location.href;

or

 location.replace(URL);

but preserving the variables u can approach querystring or session variables

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜