19

Can we get browser history for my site with Javascript ?

I can see some of old posts and threads talking about that... but those approaches didn't work for me.

Is it possible or not and how?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Mayron
  • 325
  • 1
  • 2
  • 7
  • 3
    What exaclty do you want to do? – Reporter Jul 19 '12 at 08:27
  • `"...but result is nothing"` -- result of what? Do you have some code which you tried but does not work as expected? It would be helpful if you posted that. – Shawn Chin Jul 19 '12 at 08:29
  • Possible duplicate: http://stackoverflow.com/questions/3277913/is-it-possible-to-detect-a-visitors-browsing-history-using-javascript-or-php – Anderson Green May 10 '13 at 19:03
  • I want all the history object or list like we click on history tab – ThinkTank Aug 19 '19 at 10:15
  • If you are looking a way to navigate in browser history URLs. These links would be helpful: http://www.w3schools.com/jsref/obj_history.asp, https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history/ – Riz Jul 19 '12 at 08:29

2 Answers2

6

For privacy reasons, no it is not possible to read clients browser history data.

If you are trying to observe or generate data about visitors for only on your domain, its possible to keep page information with sessions and cookies.

you can also collect click data on your websites clickable objects by your own scripts which are sending the data by ajax requests.

if you are running a one page application type website and generating your own url's with "pushState()" function, you just need to add some extra code to collect that data and send the information with ajax on page/url changes.

Serdar
  • 91
  • 1
  • 3
  • 1
    But it is possible to read the client's history from a browser extension, using the `history` API [in Chrome](https://developer.chrome.com/docs/extensions/reference/history/) or [in Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/history). – Anderson Green Sep 18 '21 at 17:33
  • 4
    @AndersonGreen i don't think its is a good practise or even possible to force people who are visiting your website to install a browser extension. – Serdar Sep 23 '21 at 07:51
  • It is probably not a good practise, but still possible, since you can [check whether an extension is installed](https://stackoverflow.com/questions/6293498/check-whether-user-has-a-chrome-extension-installed) using a script on a website. – Anderson Green Sep 23 '21 at 12:54
  • Web pages can also use [`onclick` event listeners](https://stackoverflow.com/questions/8492344/javascript-attach-an-onclick-event-to-all-links) to record the URLs of pages that their users click on. – Anderson Green May 17 '22 at 20:34
5

You can make use of History object of javascript to do that

The history property has the return value as history object, which is an array of history items having details of the URL's visited from within that window. Also, note that the History object is a JavaScript object and not an HTML DOM object.

syntax of history property of Window Object:

window.history 
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 17
    What you forgot to mention is the fact, there are only a few methods available. They allows just to navigate trough this object and replace an item. – Reporter Jul 19 '12 at 09:02
  • 4
    @Pranay: I don't know what browser tutorial you are sharing refers to, but Chrome's (27.0.1453.116 m) history object misses following properties: current, next, previous. – Marecky Jul 10 '13 at 07:39