user2304394
user2304394

Reputation: 323

window.open not working for _self

Kindly take a look at my following js function. The function works fine if i do the redirection without _self attribute. Also no matter what I did its not stopping the redirection the page itself I think that's why the _self not working and works fine for _blank.

function checkcat()
    { 
    if ($(".caty").find(".active-cat").length > 0){ 
window.open("http://www.google.com","_self");

// window.open("http://www.google.com","_blank");
}
else
{
alert('Aloha!!!');
// Following I used to strictly stop the redirection but it didnot work :(
stopPropagation();
preventDefault();
return false;

}
    }

Upvotes: 1

Views: 7003

Answers (1)

Khanh TO
Khanh TO

Reputation: 48982

To prevent the page from submitting, you need to do it like this:

function checkcat() { 
    if ($(".caty").find(".active-cat").length > 0){ 
      window.open("http://www.google.com","_self");
    } 
    else {
      alert('Aloha!!!');
    }

    return false;
}

Only return false will do the job. In your method, you do not pass in the event object and calling stopPropagation and preventDefault will throw exception as there are no such methods.

If you call checkcat inside form submit, remember to include the return to stop the form from submitting:

<form onsubmit="return checkcat()">
</form>

Upvotes: 3

Related Questions