-1

Here is an example.

1) I open google.com. Object looks like this:

{
  url: 'https://www.google.com/',
  previous: '',
  referrer: ''
}

2) Then I click on the link /news and the object looks like this:

{
  url: 'https://www.google.com/news',
  previous: 'https://www.google.com/',
  referrer: 'https://www.google.com/'
}

3) Then I open a new tab and go to https://stackoverflow.com/ and the object looks like this:

{
  url: 'https://stackoverflow.com/',
  previous: 'https://www.google.com/news',
  referrer: ''
}

How to achieve it? I tried with window.localStorage but it doesn't work.

Community
  • 1
  • 1
Green
  • 28,742
  • 61
  • 158
  • 247
  • 1
    Can please specify which "Object" you are talking about? – haffla Nov 20 '15 at 11:11
  • 2
    You cannot, think what horror it would be for your privacy... – Icepickle Nov 20 '15 at 11:13
  • @JakobPupke, object is only for the sake of demonstrativeness, visualization. You easily got the idea, don't you? So I created the object for that purpose. – Green Nov 20 '15 at 11:20
  • You can do it like this, **window.localStorage.setItem('previous',window.location.href);** and then retrieve it like this: **window.localStorage.getItem('previous');**, but it is not advisable due to security and privacy concerns as said by @Icepickle also. This [answer](http://stackoverflow.com/a/3528331/3863146) might help you. – Sahil Nov 20 '15 at 11:24
  • @Icepickle, why? `document.referrer` doesn't violates privacy but suddenly a previous page does. What is the difference? any example? – Green Nov 20 '15 at 11:27
  • @Sahil, I tried it already. But it doesn't return correct results. – Green Nov 20 '15 at 11:30
  • @Green The only thing here is that you are trying to track browser wide the history of a user. If this would be possible, suddenly all websites might have access to the users browsing history while your page is open. imho, this would be horrible for privacy reasons. The referrer is not obligatory, and will depend on a user action on another website, while you want to track the full history (new tab, different domains). I mean, you are trying to achieve something that is out of scope for a website. (nothing is stopping you from creating your own webbrowser though) – Icepickle Nov 20 '15 at 12:13

1 Answers1

1

Please try Backbone model, routing and HTML5 History API.

I am showing how to achieve the First Step:

    var model=Backbone.Model.extend({
     defaults:{
      url: '',
      previous: '',
      referrer: ''
     },
     get_url:function(){
       var self=this;
       self.get_url=window.location.href;
     }
    });

   var obj=new model(); //create instance of Model
   obj.set('url',obj.get_url); // set the url value to model attribute

if You want the previous Attribute value,

   obj.previousAttributes();

And Follow the same steps, set all other url attribute value to model and get the values.

Deepanshu Gupta
  • 240
  • 1
  • 5
  • Thanks. Maybe one day I'll try it. For now I want to implement it using pure JS without frameworks. – Green Nov 20 '15 at 12:43