Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iframe localStorage on Safari and Safari mobile

I have a javascript function that will execute in an iframe of my page to set a localstorage. On another page, another iframe hosted on the same domain will try to read the value. A schema below :

Domain A   |---- iframe - Domain B => Set localStorage  Domain C   |---- iframe - Domain B => Read localStorage 

Both localStorage function (Read and Write) are hosted on the same Domain (Domain B) On Safari and Safari mobile, the value is not transmitted from iframe in page A to iframe in page C. It's working fine on Chrome and Firefox. In Safari, it's acting as if the localStorage of the iframe is specific to the top page domain and not transmitted accross pages, even if the iframe domain is the same. Does anybody is experiencing the same problem with local storage on safari ?

like image 594
seb Avatar asked Dec 05 '13 13:12

seb


People also ask

Does iframe work in Safari?

Same-Origin policy is strictly enforced on Safari which means iframes with different domain names (or protocols or ports) will not be accessible while running automated tests.

Does Safari support localStorage?

In short, you can't, but you can set a cookie via JavaScript 😉 Safari on iOS supports localStorage, but in Private Mode it simply throws an error when you try to save anything to it, which is not great. Also it breaks the behaviour of your app on iPhones and iPads.

Is local storage shared between iframes?

According to the W3C: Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data. In other words, if the iframes all reside in the same domain, then they will share the same local storage instance.

Why iframe is not working in Safari?

Why Safari doesn't allow to store cookie for iFrame? Answer: A: Answer: A: Try going to Safari/Preferences/Privacy and uncheck Prevent cross-site tracking.


1 Answers

The issue with Safari is caused by the "block cookies and other website data" preference which is set to "From third parties and advertisers" by default.

Update: on Safari 8, this option is "Cookies and website data: Allow from websites I visit".

The expected behaviour is only available if the user sets this option to "Never", as seen in the following picture: http://i.stack.imgur.com/AP4ed.png

It's unlikely that there is a loophole unfortunately - the intention of the setting is clear.

like image 64
robC Avatar answered Oct 02 '22 05:10

robC