简体   繁体   English

返回或重定向到网址

[英]Go back or redirect to url

I've been using a back button for users to navigate back to the page they came from, problem is, if the user navigates there in a new tab or direct link, as they haven't "been anywhere" before they would get redirected back to a blank page. 我一直在使用后退按钮让用户导航回他们来自的页面,问题是,如果用户在新选项卡或直接链接中导航,因为他们在被重定向之前没有“到处”回到空白页面。

Is there a solution for this? 这有解决方案吗? I'm thinking something like navigate back if possible, and if not it'll take you to a specific url. 如果可能的话,我正在考虑像导航一样,如果没有,它会带你到一个特定的网址。

This is what I have so far: 这是我到目前为止:

<a href="#" onclick="history.go(-1);return false;">Go back</a>

How about using document.referrer ? 使用document.referrer怎么样?
It's a hack and I do not recommend it since you'll not be maintaining post data (if any). 这是一个黑客,我不推荐它,因为你不会维护发布数据(如果有的话)。 If your page doesn't need to repost any data, then this should work. 如果您的页面不需要重新发布任何数据,那么这应该可行。

<a id="mylink" href="#">Go back</a>
$('#mylink').click(function () {
    if (history.length == 0) {
        document.location = document.referrer;
    } else {
        history.go(-1);
    }
});

I must say that I agree with SchautDollar's comment - a message is a far better warning. 我必须说我同意SchautDollar的评论 - 这个消息是一个更好的警告。

If you are using php you could change link to: 如果您使用的是php,可以将链接更改为:

echo '<a href="'. $_SERVER['HTTP_REFERER'] . '">Go back</a>';

if not you can do the same with jQuery and javascript 如果不是你可以用jQuery和javascript做同样的事情

document.location = document.referrer;

on the link click method. 在链接点击方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM