Sunday, 7 May 2017

javascript settimeout onload/beforeclose dialog open new window/redirect



<input onclick="chuanshuo()" type="button" value="click this button will open new blog of chuanshuoge after 10 second" />

//wait for 10s before try to open new window.
//if new window is blocked, show dialog box,
//if confirmed, redirect to new address

<script type="text/javascript">
   function chuanshuo() {
    window.setTimeout(function f_wait() {
        var win = window.open("http://chuanshuoge1.blogspot.ca/", "chuanshuoge2");

        if (!win) {
            var dialog = confirm("chuanshuoge invites you to check his new blog.\nhttp://chuanshuoge1.blogspot.ca/");
            if (dialog == true) {
                window.location = "http://chuanshuoge1.blogspot.ca/";
            }
        }

    }, 10000);

}  
</script>

reference:

http://stackoverflow.com/questions/19851782/how-to-open-a-url-in-a-new-tab-using-javascript-or-jquery
https://www.sitepoint.com/community/t/redirect-to-another-page-on-load/2649
http://stackoverflow.com/questions/8517891/how-to-redirect-a-user-to-a-url-page-when-the-close-x-button-is-clicked
https://www.w3schools.com/jsref/met_win_confirm.asp
http://stackoverflow.com/questions/4057236/how-to-add-onload-event-to-a-div-element
http://stackoverflow.com/questions/14226803/javascript-wait-5-seconds-before-executing-next-line
http://stackoverflow.com/questions/8683528/embed-image-in-a-button-element
http://stackoverflow.com/questions/7274875/center-image-in-html-button

No comments:

Post a Comment