简体   繁体   中英

not able to redirect using javascript

I want to redirect using javascript and I am able to redirect using document.write() otherwise not.

document.write("You will be redirected to main page in 5 sec.");
setTimeout(redirect_admin(), 5 * 1000);

I want to redirect the page without using document .write() The entire code:

$(document).ready(function () {
    btnsubmit = $("#btnLogin");

    btnsubmit.click(function () {
        //$("#preloader").show();
        //$("#status").show();
        var uName = $("#userName").val();
        var uPass = $("#pasWord").val();
        var str = -1;
        $.ajax({
            type: "Post",
            async: false,
            url: "Default.aspx/userLogin",
            data: '{"userName":"' + uName + '","userPassword":"' + uPass + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                str1 = response.d;
                //$('#status').delay(300).fadeOut(); // will first fade out the loading animation
                //$('#preloader').delay(350).fadeOut('slow');
                str = str1;
            },

            failure: function (msg) {
                bootbox.alert("Please contact your administrator");
            }
        });
        redirectUser(str);
    })


    redirectUser = function (str) {
       if(str == 1){}
       ....
       else if(str ==6) {
          document.write("You will be redirected to main page in 5 sec.");
            setTimeout(redirect_admin, 5 * 1000);
        }

       function redirect_admin() {
        var nextUrl = location.protocol + "//" + location.host + "/AdminHome.aspx";
        window.location = nextUrl;
    }

Don't use () next to the function name.

setTimeout(redirect_admin, 5 * 1000);

Hope it helps:)

Adding to that, I am not sure in what way document.write is stopping you from getting things working. You can use a modal or ajax-loader or even an alert instead of document.write. If this is what you mean.

The below code is working for me

 $(document).ready(function () {
            $("#N").click(function () {

   setTimeout(function () {redirect_admin();}, 5000);
            });
        });

function redirect_admin() {

            window.location = "https://www.google.com";
        }

1. Make sure your else condition block executed

else if(str ==6) {
    console.log(str) 
    document.write("You will be redirected to main page in 5 sec.");
    etTimeout(redirect_admin, 5 * 1000);
}

2. in function redirect_admin change window.location to window.top.location if you in i-frame and want to redirect top frame

function redirect_admin() {
    var nextUrl = location.protocol + "//" + location.host + "/AdminHome.aspx";
    window.top.location = nextUrl;
}

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