prog1011
prog1011

Reputation: 3495

How to Prevent to redirect page before javascript alert click?

I am using below javascript code. I want to stay on same page till user click on alert. After click on alert page should redirect to SomePage.aspx

<script type="text/javascript">
    function myFunction() {
        alert("Some text...");
        window.location = 'SomePage.aspx';
    }
</script>

Please let me know what I missed here. Thanks.

Upvotes: 0

Views: 2352

Answers (2)

sagar shah
sagar shah

Reputation: 1

Try Like this

Script

<script type="text/javascript">
window.alert = function (al, $) {
            return function (msg) {                
                al.call(window, msg);                
                $(window).trigger("okbuttonclicked");
            };
        }(window.alert, window.jQuery);

        $(window).on("okbuttonclicked", function () {
            redirect();
        });

        function redirect() {
            window.location.href = "http://www.google.com";
        }
        function myFunction() {
            alert("Some text...");
        }
</script>

Html

<input type="button" onclick="myFunction()" value="Click" />

Note :- This function is execute on every alert ok button. But you also pass some additional parameter for preventing to execute. And i also recommended to use confirm instead of this.

Upvotes: 0

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

Instead of alert i think you need confirm box :-

<script type="text/javascript">
function myFunction() {
    var conf = Confirm("Some text...");
    if(conf == true){
    window.location = 'SomePage.aspx';
    }
    else{ }
}
</script>

Upvotes: 2

Related Questions