user1873073
user1873073

Reputation: 3670

Getting browser history with Javascript?

This question has been asked many times but most of those questions are old.

I have tried several different techniques:

a:visited {} /* read computed style - always returns :link color*/
a:visited {} /* set height and measure that, turns out you can only set various colors */
a:link:after { content:"abc"} /* tried various styles */
/* the same restrictions apply when dealing with nested/child elements */

I've tried taking a "screenshot" of the div and putting it into the canvas in order to get pixel colors that way.

I've considered hovering a translucent div over an anchor and somehow using that to measure the color.

I've tried loading a stylesheet from a site you want to sniff and timing how long it takes (on first/second/... load) but the results are weird (sometimes it loads faster the first time like its actually faster over the network than locally cached or something).

<script>
    var t = new Date().getTime();
</script>
<link id="test" rel="stylesheet" type="text/css" href="http://l.yimg.com/zz/combo?nn/lib/metro/g/breakingnews/breakingnews_0.0.49.css" />
<script> 
    document.getElementById("test").onload = function () {              var ft = ((new Date().getTime()) - t) + "ms";
        alert(ft);
    };
</script>

Mozilla outlines the why and hows of these security restrictions here.

Is history sniffing completely impossible and if so are there any standard/accepted/user-friendly ways of doing it?

Upvotes: 1

Views: 3140

Answers (2)

dresende
dresende

Reputation: 2230

As pointed out by @boisvert, it's a privacy violation. It was once possible but not on recent browser versions. It's probably better to try saving clicked links on localStorage and check that later. It's not 100% effective but with javascript on the browser side you're never 100%.

As a site node, alternatively you might want to do it server side. That would be 100% effective.

Upvotes: 1

boisvert
boisvert

Reputation: 3739

As the comments highlight, you won't be able to obtain browser history information - at least, not if browsers are designed as they should - because it's a privacy violation.

There are some limited options:

  • The history object has back, forward and length methods for navigating back and forwards in the current window or frame; but (privacy again) it is readable, so you cannot do things like x = window.history[0]
  • Using cookies (or sessions) you could maintain navigation history on your site, and rely on that information for user-friendly features, limited to the site that you read cookies/session data on.
  • We could imagine a multisite version by having every link redirected via a central site first, which would record history, and that site hosting a javascript library that provides features for visited links. It still relies on every link redirected through a central place for history keeping: so it's only applicable to sites that consent to that.

Upvotes: 1

Related Questions