简体   繁体   中英

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?

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(); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM