-1

I need the js function to go back in history to the last pages used in a specific domain.

reason: I send people to a subdomain to see specific content-galleries (gallery.mydomain.xyz/#1etc). I need them to return to the last page where they left of from the specific tld (mydomain.xyz/pageX) after having clicked through a number of images/videos there at subdomain...

is this possible? any ideas? thx!

pnuts
  • 58,317
  • 11
  • 87
  • 139
oliver
  • 1

3 Answers3

1

It's not possible using the built-in browser history, no, because your access to that from JavaScript is close to non-existent.

What you can do instead is save the location you want to take them back to in, say, session storage (use local storage instead if this is in a different window), and then link back to that page.

More about session storage / local storage in the web storage spec, but the short version is that it stores strings in a storage area specific to your origin, so for instance:

localStorage.setItem("last-location", "foo");

...stores "foo" in local storage for your origin, with the key "last-location". You can then use getItem to get it later when you need it:

var lastLocation = localStorage.getItem("last-location");
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • It's also possible to [share local storage between domains](https://stackoverflow.com/questions/33957477/cross-domain-localstorage-with-javascript), in order to access the location from another domain. – Anderson Green May 03 '22 at 22:16
  • 1
    @AndersonGreen - Not really. It's possible to do *messaging* cross-domain with [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage), and of course if you have a page open in each domain, they each have access to their own local storage and can use that information when messaging each other. That's not shared local storage, it's messaging. – T.J. Crowder May 04 '22 at 06:17
0

you could use a simple get/post variable to tell where the user is coming from and store that in a session variable for later use when the user is to be returned. As far as I know you cant access the users browsing history from the browsing client with Javascript as its a violation of the sandbox design but that may have changed recently

FroboZ
  • 437
  • 1
  • 6
  • 17
0

thx both of you for the quick answer! ... I kind of see, not being a versatile coder myself. but I get the problem involved. and see session-storage is where I want to look at then...

I will have to make this a coding job given my non-skills here :-}

but now I know what to ask for. thx again.

oliver
  • 1