简体   繁体   English

如何使用JavaScript获取上一页URL,不同域名

[英]how to get previous page URL, different domain name using javascript

Is it possible to get the url of the previous website page from another website domain using javascript, example: 是否可以使用javascript从另一个网站域获取上一个网站页面的网址,例如:
user have this website url in browser: 用户在浏览器中具有以下网站网址:

www.example.com/page1.html

then user cleared the browser address bar and wrote my website 然后用户清除浏览器地址栏并写了我的网站

url: www.test.com

What I need is using javascript to get the 我需要使用javascript来获取

www.example.com/page1.html url.

Is that possible? 那可能吗?

No, for the same reasons you can not get the url from an iframe if it's on a different domain: cross-domain security. 不,出于相同的原因,如果iframe位于其他域中,则无法从iframe获取该网址:跨域安全性。

What you can do is get the $_SERVER['HTTP_REFERER'] in php but it's not always set... Or as @FabrícioMatté commented document.referrer in javascript, but reliability is just as bad. 您可以执行的操作是在php中获取$_SERVER['HTTP_REFERER'] ,但并不总是将其设置为...或作为@FabrícioMatté在javascript中评论了document.referrer ,但可靠性同样很差。

The address of the page (if any) which referred the user agent to the current page. 页面(如果有的话)的地址,该页面将用户代理引至当前页面。 This is set by the user agent. 这是由用户代理设置的。 Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. 并非所有的用户代理都将设置此功能,有些用户代理提供了将HTTP_REFERER修改为功能的功能。 In short, it cannot really be trusted. 简而言之,它不能真正被信任。

http://php.net/manual/en/reserved.variables.server.php http://php.net/manual/en/reserved.variables.server.php

To go back to the previous page you can use history.go(-1) in javascript. 要返回上一页,您可以在javascript中使用history.go(-1)

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

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