Rees Gargi Rees Gargi - 4 months ago 10
Javascript Question

Can I get setTimeOut() executed after page redirection?

Why not Duplicate: Question wasn't answered in previous post.

I have a delete link, which when get clicked triggers a popup which contains a button. When I click that button, then the page (inside the popup) gets redirected to a new internal page. What I want to achieve is that the pop-up should close after 1 second after the redirection has happened. I applied the function when the button is clicked which is only available before the "Popup Redirection".

$('.page-user-cancel .btn').click(function(e) {
window.onunload = refreshParent;
setTimeout( function(){
function refreshParent() {
window.opener.location.reload();
window.close();
}
} , 1000 );
});


If I am not clear, please comment. English is not my native language.

EDIT

If that is not feasible, is it possible to trigger the function when the div on the new page (after redirection) gets loaded?

UPDATE

This isn't working:

var dival = $('.inside .alert').length;
if (dival > 0){
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
window.close();
}
}
else{}


It works, only after refreshing.

Got it Worked: Had to remove the function call of refreshParent. might not be the best practice, but it is working that way.

Answer

The first part is not possible since, reloading the page doesn't keep JavaScript of the previous page running.

Second part is better. Yes, you can handle page load:

$( document ).ready(function() {
  // Check your div here
});
Comments