CaffeineShots
CaffeineShots

Reputation: 2162

how to run script onbeforeunload

hi somebody pls explain to me why this code of mine doesn't work

    $(document).ready(function()
    {wireUpEvents(); 
    });

    function  wireUpEvents()
    {window.onbeforeunload = function()
        {
        window.location="link"; // on before unload i will run this link first.
        }
    }

when i refresh my page this code will run but it'll not go to link(window.location) before load however when i add alert like this

window.location="link"; alert("anything");

it'll go to my link before load. I like to eliminate that weird alert().

pls feel free to suggest any treatment with my code. thanks

Upvotes: 2

Views: 2925

Answers (2)

Ripa Saha
Ripa Saha

Reputation: 2540

try below code

window.onbeforeunload=testfunc;

function testfunc()
{
window.location="link";
}

Upvotes: 2

Prasanth Bendra
Prasanth Bendra

Reputation: 32820

Try this :

<script type="text/javascript"> 
    $(window).bind('beforeunload', function() { 
            window.location="link"; 
        } 
    );
</script>

Upvotes: 1

Related Questions