我想使用javascript重定向,我可以使用document.write()重定向,否则无法重定向。
document.write("You will be redirected to main page in 5 sec.");
setTimeout(redirect_admin(), 5 * 1000);
我想在不使用文档.write()的情况下重定向页面 整个代码:
$(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;
}
答案 0 :(得分:0)
不要在函数名旁边使用()。
setTimeout(redirect_admin, 5 * 1000);
希望有所帮助:)
除此之外, 我不确定document.write以什么方式阻止你让事情起作用。您可以使用模态或ajax-loader甚至是alert而不是document.write。如果这就是你的意思。
答案 1 :(得分:0)
以下代码对我有用
$(document).ready(function () {
$("#N").click(function () {
setTimeout(function () {redirect_admin();}, 5000);
});
});
function redirect_admin() {
window.location = "https://www.google.com";
}
答案 2 :(得分:0)
<强> 1。确保执行其他条件阻止
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。在函数redirect_admin
中,如果您在i-frame中并且想要重定向顶部框架,则将window.location
更改为window.top.location
function redirect_admin() {
var nextUrl = location.protocol + "//" + location.host + "/AdminHome.aspx";
window.top.location = nextUrl;
}