5

I have created a JS file that I place in some webpages other than mine. So mine is domain-1.com and I place this to domain-2.com and domain-3.com

This JS contains jsonp and I save some data from their pages to my database successfully. Also, I create some cookies and I save a value to the localstorage. the problem is that when a visitor goes to domain-2.com and tomorrow to www.domain-2.com they will have a different value because os the www.

I want this value to be the same across www. or not, maybe at the same time, I do not know an applicable idea. It is better for me to pass the value the same time for www. and without www.

How to do this? I only provide them with a JS external link. It is ok If I place an iframe also.

EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

1 Answers1

7

The best solution would be to set a redirect to either of the domains so you can avoid this problem altogether.

The following code shows the concept of sending values to the non-www domain for storage only. If you need to read those values from the www domain too or want a library to do everything for you, you should use one of the libraries listed at the end. Those libraries use the same concept but will handle most things for you.


You can store the value on one domain only and use cross-origin communication to send the value if you are on the www domain. Create an iframe that loads a script of the non-www domain. In this script you save the value in the local storage of that domain.

Here is the content of the iframe with some minimal html5 markup, in this example saved as storage.html and served from example.com.

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title> </title>
<script>
window.addEventListener("message", storeItem, false);
function storeItem(event) {

    if (!(event.origin == "http://example.com" || event.origin == "http://www.example.com")) {
        return;
    }

    localStorage.setItem(event.data.key, event.data.value);

}
</script>
</head></html>

When you want to store data use postMessage to communicate with the iframe. The iframe needs to be loaded before you can send messages.

<iframe id="storageScriptFrame"></iframe>

<script>

var target = "http://example.com";
var storageScriptFrame = document.getElementById("storageScriptFrame");
var storageScriptWindow = storageScriptFrame.contentWindow;


function storeItem(key, value) {
    var message = {key: key, value: value};
    storageScriptWindow.postMessage(message, target);
}


// you can store values after the iframe has loaded
storageScriptFrame.onload = function() {

    storeItem("foo", "bar"); 

};
// replace this with actual page
storageScriptFrame.src = 'http://example.com/storage.html'; 

</script>

Make sure to replace the example.com domain with the actual domain. Checking the origin domain is important so other sites can't send you messages.


At some point you will also want to read those stored values. Depending on what you do with the stored values, you have two options.

  • If you don't need to interact with the main window, you can move the script that reads values into the iframe.
  • If you do need to get the value on the main window, use postMessage again to send values back.

The second option can get complicated though, because postMessage is asynchronous and only works one way. I would recommend to use an existing library to do this (you don't need the code above then).

For example if you Cross Domain Local Storage you simply need to follow the setup instructions and in the initCallback function you can call xdLocalStorage.getItem and xdLocalStorage.setItem to get and set items from the localstorage of example.com.

kapex
  • 28,903
  • 6
  • 107
  • 121
  • Thank you for the logic but I do not know how to implement this, please let me know where do I put each block of code and where do I put `localStorage['abc'] = 'value'` – EnexoOnoma Sep 07 '14 at 12:42
  • Please provide me full code even for the iframe, something that +300 worth it – EnexoOnoma Sep 07 '14 at 12:47
  • @Xalloumokkelos I've added the complete code. Now after looking around for existing solutions I've found some libraries which seem to do the same thing and more. Both libraries seem to have simple instructions how to use them. – kapex Sep 07 '14 at 18:16
  • Hello there, I just tested your code. What I noticed is that when i visit `www.domain.com` it creates a localstorage for `domain.com` showed on `www.domain.com` and `domain.com`. When I visit `domain.com` it does not create anything... So, because I read the value, on www. it displays nothing, but the value is shown when I visit `domain.com` (without www). How to fix this? – EnexoOnoma Sep 09 '14 at 20:08
  • Sorry, I forgot to add `example.com` to the origin check so it allows messages from both domains. That should fix it, though I'm not sure what you mean by "it creates a localstorage for domain.com showed on www.domain.com and domain.com". If by "show" you mean "read the value from localstorage", that shouldn't be possible. Visiting any of the domains should only save to the localstorage of `domain.com`. That storage is only readable from `domain.com` (which means it is readable from the iframe). Reading from `www.domain.com` would be possible with more complex code or one of the libraries. – kapex Sep 09 '14 at 20:59
  • Thank you for the update. Please tell me, when I am at www.domain.com can i read the value of the domain.com iframe? How? – EnexoOnoma Sep 10 '14 at 08:01
  • I thought I made that clear, to read the value from `www.domain.com` please just use one of the libraries. I _could_ write code for this but I would essentially duplicate one of the libraries, so I won't do that. The libraries have been tested way more than my code and the authors probably have put more thought into it than me too. – kapex Sep 10 '14 at 12:16
  • Sorry I think my last comment sounded a bit irritated but that wasn't my intention. I really just meant to say the libraries are probably better and more reliable than my code. Good luck with you project :) – kapex Sep 11 '14 at 10:21
  • hehe nooooo, you are great kapep, helped me a lot i didn't mean anything! :) – EnexoOnoma Sep 11 '14 at 12:56
  • zendesk's cross-storage looks like the standard library for this now, https://github.com/zendesk/cross-storage – Henrik Karlsson Jun 06 '16 at 15:49