javascript 我如何从 history.go(-2) 获取 url

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

how can i get the url from history.go(-2)

javascript

提问by sebastian

I have a question, is it possible to get the url from the history.go(-2)? Assuming i don't know that url.

我有一个问题,是否可以从 history.go(-2) 中获取 url?假设我不知道那个网址。

edit

编辑

I need the url, because I want to check where the visitor was redirected from. there are two cases: 1 facebook, 2 directly on the website. the second step is an automatic user check. the third step is miscoded, but if I go back to step 1. I can bypass the error. with everything working fine. the problem is that I don't know where to redirect (being two forwarders).

我需要 url,因为我想检查访问者是从哪里重定向的。有两种情况:1 facebook,2 直接在网站上。第二步是自动用户检查。第三步是错误编码,但如果我回到第一步。我可以绕过错误。一切正常。问题是我不知道重定向到哪里(作为两个转发器)。

回答by Edgar Bonet

It is not, by design, for privacy reasons.

出于隐私原因,这并非有意设计。

回答by VoteyDisciple

You can in some special circumstances get the previous URL with history.previous(and likewise for .currentand .next) but you can't look back arbitrarily and see where I (the visitor) went two or three or ten pages ago. From a website, reliably getting even the most recent page URL (which is sometimes available as the "referer") is never guaranteed.

在某些特殊情况下,您可以使用history.previous(以及.current.next)获取先前的 URL,但您不能随意回顾并查看我(访问者)两、三或十页前去了哪里。从网站上,即使是最新的页面 URL(有时可用作“引用者”)也无法保证可靠地获取。

Thinking about it from your visitor's perspective, this is good. Just because I'm visiting your site now doesn't mean I want you to know about every other page I've ever visited in the same browser window.

从访问者的角度考虑,这很好。仅仅因为我现在正在访问您的网站并不意味着我希望您了解我在同一浏览器窗口中访问过的所有其他页面。

回答by bobince

No, not in general.

不,不是一般的。

If the URL is on the same hostname as the current page you sort of could, by opening a pop-up window, navigating back 2, then using script in the pop-up to read the opener.location, navigate forward 2, and pass the URL to script in the opener. But it would be an enormous pain and pretty unreliable.

如果 URL 与当前页面在同一主机名上,您可以通过打开一个弹出窗口,向后导航 2,然后使用弹出窗口中的脚本读取opener.location,向前导航 2,然后将 URL 传递给中的脚本opener。但这将是一个巨大的痛苦并且非常不可靠。

If the URL isn't on the same hostname, you can't at all, for privacy reasons.

如果 URL 不在同一个主机名上,出于隐私原因,您根本不能这样做。

回答by Richard Marskell - Drackir

I'm not sure if this is possible with your set up, but you could add a GET veriable (ie. page.ext?ref=1) and then store that in a session and output it on whatever page you need. Then, on your facebook page, you could add ?ref=1to the link. This would allow you to add other site references in the future as well.

我不确定您的设置是否可行,但您可以添加一个 GET veriable(即page.ext?ref=1),然后将其存储在会话中并将其输出到您需要的任何页面上。然后,在您的 facebook 页面上,您可以添加?ref=1到链接。这也将允许您在未来添加其他站点引用。

回答by Buhake Sindi

It probably isn't possible as history.go()does a URL redirection.

它可能history.go()不像 URL 重定向那样可能。

You can get the previous/next URL by doing history.previous/history.nextproperty respectively. This returns you an absolute URL. For current URL, use history.current.

您可以通过分别执行history.previous/history.next属性来获取上一个/下一个 URL 。这将返回一个绝对 URL。对于当前 URL,使用history.current.

More info on history object.

有关历史对象的更多信息。