Javascript 如何设置javascript重定向以将用户发送到后台?看代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2326449/
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-22 23:48:50  来源:igfitidea点击:

How to setup javascript redirect to send user to back page? see code

javascriptjquery

提问by user187580

With onclick="history.go(-1);return false;"user can navigate to back pages. But if I want to put a confirm method before it takes user to the back. How would I do that?

随着onclick="history.go(-1);return false;"用户可以导航到后页。但是如果我想在将用户带到后面之前放置一个确认方法。我该怎么做?

I think it can be achieved by doing something like below but I am not sure how to redirect user to back page?

我认为可以通过执行以下操作来实现,但我不确定如何将用户重定向到后退页面?

$('.clickme').click(function() {
    if(confirm("Are you sure you want to navigate away from this page?"))
    {
      //window.close();
      // how to redirect to history.go(-1) ??
    }        
    return false;
 });

UPDATE

更新

Also is there any way I can check if history has some values in it or is empty? So that I can alert user if is empty?

还有什么方法可以检查历史记录中是否有一些值或为空?这样我就可以在为空时提醒用户?

回答by dxh

It's quite as simple as you think it is:

它和你想象的一样简单:

$('.clickme').click(function() {
   if(confirm("Are you sure you want to navigate away from this page?"))
   {
      history.go(-1);
   }        
   return false;
});

回答by Sarfraz

Try this:

尝试这个:

    if(confirm("Are you sure you want to navigate away from this page?"))
    {
      history.back();
    }     

回答by Dodswm

$('.clickme').click(function() { 
        if(history.length != 0)
        {
            if(confirm("Are you sure you want to navigate away from this page?")) 
            { 
                history.go(-1);
            }
         return false;
        }
        alert("No history found");
        return false;
});

回答by user7868

$('.clickme').click(function() { 
    if(history.length != 0)
        {
            if(confirm("Are you sure you want to navigate away from this page?")) 
            { 
                document.location.replace(document.referrer)
            }
        return false;
        }
        alert("No history found");
        return false;
});

(This code draws heavily on Dodswarm's answer.)

(此代码大量借鉴了 Dodswarm 的答案。)

回答by santosh Kundkar

<script type="text/javascript" language="javascript"> 

function goBack() 
{
 window.history.back()
}

window.onkeyup = function (e) 
{
 if (e.keyCode == 27) window.history.back();
}
</script>

Add above code in your head section of page. Here whenever user press Escbutton then he will be redirected to previous view page and if we want to add this logic on back button press then just call goback()on click event.

在页面的头部部分添加上面的代码。在这里,每当用户按下Esc按钮时,他将被重定向到上一个视图页面,如果我们想在后退按钮按下时添加此逻辑,则只需调用goback()单击事件即可。