0

What I have below is a script that passes a variable from the crossvalue.html to an iframe crossvaluePOST.html and creates a localstorage to the (without www) domain.com

My question is how can I create the same localstorage value for the www.domain.com when domain.com/crossvalue.html (without www) runs?

this is crossvalue.html

<iframe id="da-iframe" src="crossvaluePOST.html"></iframe>

<script>
window.onload = function () {
    var iframeWin = document.getElementById("da-iframe").contentWindow; 

myMessage= Math.floor(Math.random()*801);
        iframeWin.postMessage(myMessage, "http://domain.com");
        return false;

};
</script>

and this is the crossvaluePOST.html

<script>

function displayMessage (evt) {
    var message;
    if (evt.origin !== "http://domain.com")  {
        message = "You are not worthy";
    }
    else {

        localStorage['abc'] =  evt.data;
    }   
}

if (window.addEventListener) {
    // For standards-compliant web browsers
    window.addEventListener("message", displayMessage, false);
}
else {
    window.attachEvent("onmessage", displayMessage);
}
</script>
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • The question is: "Why?" The correct way to handle this would be to always redirect `www.domain.com` to `domain.com` or the way round. Never copy content. – Johannes H. Sep 07 '14 at 14:12
  • @JohannesH. I agree with you, but in this case it is something that I do not have permissions since I provide other webpages with an external code. – EnexoOnoma Sep 07 '14 at 14:14
  • I'm not exactly sure if the same origin policy lets you access local storage of other subdomains. I never tried this tbh, but one approach could be to try and load a javascript file from the www-prefixed domain to create the storage? – Johannes H. Sep 07 '14 at 14:18
  • @JohannesH. what I follow is this logic http://stackoverflow.com/questions/4026479/use-localstorage-across-subdomains but I do not know if I am correct – EnexoOnoma Sep 07 '14 at 14:19
  • I just read that post, actually. What you do is different. You need to load `www.domain.com/crossvaluePOST.html` on the non-prefixed site, and the way round. – Johannes H. Sep 07 '14 at 14:20
  • @JohannesH. I think the solution you are suggesting is too simple that it messes with my head and it is annoyingly funny. Can you provide me with an answer, I will accept it. – EnexoOnoma Sep 07 '14 at 14:33
  • I try to avoid posting suggestions as answers unless I can confirm they actually work. But I'll try. – Johannes H. Sep 07 '14 at 14:36

1 Answers1

0

According to "use localStorage across subdomains" (which you linked yourself), the way to do this is to load a page from the other subdomain into your page, and communicate with that page to enter the values. This is necessary because of same-origin policy: a script on a webpage is only allowed to access localstorage from the domain the site was loaded from.

In your case that means, that domain.com/crossvaluePOST.html cannot access the localstorage for www.domain.com, but only domain.com. TO change this, you need to load www.domain.com/crossvaluePOST.html in domain.com/crossvalue.html (and domain.com/crossvaluePOST.html in www.domain.com/crossvalue.html if you need both directions).

Just change your crossvalue.html to

<iframe id="da-iframe" src="//www.domain.com/crossvaluePOST.html"></iframe>

<script>
    // ...
</script>

to write the www.domain.com localstorage when the page is accessed using domain.com (if you need both directions, you obviously have to change the URL based on where the page is loaded from)

Disclaimer: this is just taken from the post you linked, I never tried this.

Community
  • 1
  • 1
Johannes H.
  • 5,875
  • 1
  • 20
  • 40