使用 setTimeout 进行 Javascript 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11960981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:50:35  来源:igfitidea点击:

Javascript Redirect using setTimeout

javascriptredirectsettimeout

提问by dpDesignz

I have a button that I want to run a piece of script and then redirect, however nothing is happening

我有一个按钮,我想运行一段脚本然后重定向,但是什么也没发生

I have another page that uses similar code and that works. but this one just isn't working

我有另一个页面使用类似的代码并且可以工作。但这只是行不通

Code to work:

工作代码:

<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "confirm_order", id: "<?php echo $o_id; ?>", name: "<?php echo $_SESSION['user_name']; ?>" email: "<?php echo $_SESSION['user_email']; ?>"};setTimeout("window.location.href=order.php?order_id=<?php echo $o_id;?>", 100);' class="button">

Code that already works on a different page:

已经在不同页面上运行的代码:

<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "remove", id: "<?php echo $o_id; ?>", prod: "<?php echo $row_prod['prod_id']; ?>" });setTimeout("window.location.href=window.location.href", 100);'>

Now I know it's all to do with my setTimeout, I'm just not sure what I'm doing wrong.

现在我知道这完全与我的 setTimeout 有关,我只是不确定我做错了什么。

EDIT

编辑

Link now:

现在链接:

<a href="javascript:void(0);" class="button confirm">Confirm</a>

Code below the link:

链接下方的代码:

<script type="text/javascript">
$('a .confirm').on('click',function(e){
    $.get("features.php",{
        cmd: "confirm_order",
        id: "<?php echo $o_id; ?>",
        name: "<?php echo $_SESSION['user_name']; ?>",
        email: "<?php echo $_SESSION['user_email']; ?>"
    });
    setTimeout(
        function(){
            window.location = "order.php?order_id=<?php echo $o_id;?>" 
        },
    100);
});
</script>

which still isn't working, and is rendering the \"in the code

仍然无法正常工作,并且正在\"代码中呈现

回答by Isaac Gonzalez

I agree with Felix Kling comment that you shouldn't add that much code to an html attribute. I see that you're using jQuery so why don't you just add this to your javascript code:

我同意 Felix Kling 的评论,即您不应该向 html 属性添加那么多代码。我看到您正在使用 jQuery,那么为什么不将其添加到您的 javascript 代码中:

$('a .button').on('click',function(e){
    $.get("features.php",{ 
          cmd: "confirm_order",
          id: "<?php echo $o_id; ?>",
          name: "<?php echo $_SESSION['user_name']; ?>", // you were missing this comma 
          email: "<?php echo $_SESSION['user_email']; ?>"
        }).done(function(){
             window.setTimeout( function(){
                 window.location = "order.php?order_id=<?php echo $o_id;?>";
             }, 100 );
        }); // you where missing this parenthesis          

  e.preventDefault(); 
});

回答by 1' OR 1 --

Try putting your file name in between "(You can escape it using \"):

尝试将您的文件名放在两者之间"(您可以使用 将其转义\"):

setTimeout("window.location.href=\"order.php?order_id=<?php echo $o_id;?>\";", 100);

Your file name is a string. Otherwise JavaScript would expect a variable. That is why setTimeout("window.location.href=window.location.href;", 100);works.

您的文件名是一个字符串。否则 JavaScript 会期望一个变量。这就是为什么setTimeout("window.location.href=window.location.href;", 100);有效。

I think there is also one comma missing:

我认为还缺少一个逗号:

name: "<?php echo $_SESSION['user_name']; ?>" email: "<?php echo $_SESSION['user_email']; ?>"

should be name: "<?php echo $_SESSION['user_name']; ?>", email: "<?php echo $_SESSION['user_email']; ?>"

应该 name: "<?php echo $_SESSION['user_name']; ?>", email: "<?php echo $_SESSION['user_email']; ?>"

You are also missing a parenthesis. Try to change your code to:

您还缺少一个括号。尝试将您的代码更改为:

<a href="javascript:void(0);" onclick='$.get("features.php",{ cmd: "confirm_order", id: "<?php echo $o_id; ?>", name: "<?php echo $_SESSION['user_name']; ?>", email: "<?php echo $_SESSION['user_email']; ?>"});setTimeout("window.location.href=\"order.php?order_id=<?php echo $o_id;?>\";", 100);return false;' class="button">

回答by Engr Zardari

try out this

试试这个

<script type="text/JavaScript">
redirectTime = "1500";
redirectURL = "http://www.natural-environment.com";
function timedRedirect() {
    setTimeout("location.href = redirectURL;",redirectTime);
}
</script>