cantread
cantread

Reputation: 405

Javascript redirect is only refreshing page

I have a javascript function that looks like:

function logout() {

alert('logging out 2');
window.location.replace("http://stackoverflow.com");

}

The alert pops up but the page refreshes when I want it to go to stackoverflow.com (for testing purposes). Why would this be?

Upvotes: 0

Views: 118

Answers (1)

epascarello
epascarello

Reputation: 207501

My guess is you are not cancelling the click event of the element that is calling the function so the page is refreshing. Cancel the click event.

onclick="logout(); return false;"

or use preventDefault


From the comments

$('#logout').click(function(e) {e.preventDefault(); logout(); });

Upvotes: 4

Related Questions