从window.history查找以前的URL [英] Finding previous URL from window.history
问题描述
我经常 API,浏览器地址栏中的URL从以下数据可以明显看出:
> JSON.stringify(window.history)
{state:null,length:2}
现在的问题是,我的书签不起作用,因为 window.location.href
指向data:text / html,chromewebdata
一旦发生这种情况。
我看了这个问题你如何获得Javascript中的前一个url?接受的答案是非常不正确的。所以,在我的情况下, document.referrer
是空的。
window.history
? 找到一种方式(至少在它持续的时间内!)
Chrome将整个网址设置在 document.title
,所以查询它就是需要的。如果任何人有兴趣,这里是修改过的代码:
pre $ (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)))
}
})()
注意:双 unescape
需要通过Google广告链接。
I've hosts file blocking so time to time, I get these "Page not found" errors browsing thru deals.
Since I got tired of copying the target url, unescaping, replacing it in address bar and hitting enter, I wrote a handy bookmarklet to automate this:
(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));
}
})()
Now the problem is that Chrome, internally redirects and unreachable page to its own bounce.php
which produces the following error page.
Since it supports history API, the URL in browser address bar doesn't change, as evident from the following data:
> JSON.stringify(window.history)
{"state":null,"length":2}
Now the problem is, my bookmarklet doesn't work since window.location.href
points to "data:text/html,chromewebdata"
once this happens.
I've looked at this question How do you get the previous url in Javascript? whose accepted answer is blissfully incorrect. Rightfully so, document.referrer
is empty in my case.
So is there a way to find the previous URL from window.history
? window.history.previous
is non-standard and doesn't work on Chrome anyway.
Found a way (at least while it lasts!)
Chrome sets the entire URL in document.title
, so querying it is all that's needed. Here's the modified code if anyone's interested:
(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)))
}
})()
Note: The double unescape
is needed for links coming via Google Ads.
这篇关于从window.history查找以前的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!