[英]Finding previous URL from window.history
我托管了文件阻止功能,因此有时会出现通过交易浏览这些“找不到页面”的错误。
由于我已经厌倦了复制目标URL,转义,在地址栏中替换目标URL并按Enter键,因此我编写了一个方便的书签来使其自动化:
(function () {
var href = window.location.href;
var loc = href.indexOf('url=');
if (loc > 0) {
var endLoc = href.indexOf('&', loc + 4);
endLoc = endLoc > 0 ? endLoc : href.length;
window.location.href = unescape(href.substring(loc + 4, endLoc));
}
})()
现在的问题是,Chrome浏览器在内部将页面重定向到其自身的bounce.php
并生成以下错误页面。
由于它支持历史记录 API,因此浏览器地址栏中的URL不会更改,如以下数据所示:
> JSON.stringify(window.history)
{"state":null,"length":2}
现在的问题是,一旦发生这种情况,我的小书签将不起作用,因为window.location.href
指向"data:text/html,chromewebdata"
。
我已经看过这个问题了, 如何在Javascript中获取先前的URL? 他接受的答案很不正确。 正确的是,我的情况下document.referrer
为空。
那么有没有办法从window.history
查找以前的URL? window.history.previous
是非标准的,无论如何在Chrome上均不起作用。
找到了一种方法(至少持续的时间!)
Chrome会在document.title
设置整个URL,因此只需查询即可。 如果有人感兴趣,这是修改后的代码:
(function () {
var href = document.title;
var loc = href.lastIndexOf('http');
if (loc > 0) {
var endLoc = href.indexOf(' is not available', loc);
endLoc = endLoc > 0 ? endLoc : href.length;
window.location.href = unescape(unescape(href.substring(loc, endLoc)))
}
})()
注意 :对于通过Google Ads链接的链接,需要使用双重unescape
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.