Go to URL after OK button if alert is pressed

后端 未结 7 549
逝去的感伤
逝去的感伤 2020-12-01 17:33

I need to make sure that when the user clicks OK in a JavaScript alert window, the browser moves to a different URL. Is this possible?

相关标签:
7条回答
  • 2020-12-01 18:28

    If it is for accessibility and you want to listen to every link on the page and than check if you are leaving the current site to another domain, check out what I wrote, expanding on Joe's answer

            $('a').on('click', function() {
    
                if ( this.host !== window.location.host ) {
                    if ( window.confirm('Really go to another page?') ) {
                        // They clicked Yes
                        console.log('you chose to leave. bye.');
                    }
                    else {
                        // They clicked no
                        console.log('you chose to stay here.');
                        return false
                    }
                }
            }); 
    
    0 讨论(0)
提交回复
热议问题