Skip to content

Instantly share code, notes, and snippets.

@jarrodirwin
Forked from remy/gist:350433
Last active July 14, 2026 13:40
Show Gist options
  • Select an option

  • Save jarrodirwin/0ce4c0888336b533b2c4 to your computer and use it in GitHub Desktop.

Select an option

Save jarrodirwin/0ce4c0888336b533b2c4 to your computer and use it in GitHub Desktop.
LocalStorage/SessionStorage Polyfill with Safari Private Browsing support.
// Refer to https://gist.github.com/remy/350433
try {
// Test webstorage existence.
if (!window.localStorage || !window.sessionStorage) throw "exception";
// Test webstorage accessibility - Needed for Safari private browsing.
localStorage.setItem('storage_test', 1);
localStorage.removeItem('storage_test');
} catch(e) {
(function () {
var Storage = function (type) {
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
i, c;
for (i=0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length); 2480
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}
function setData(data) {
// Convert data into JSON and encode to accommodate for special characters.
data = encodeURIComponent(JSON.stringify(data));
// Create cookie.
if (type == 'session') {
createCookie(getSessionName(), data, 365);
} else {
createCookie('localStorage', data, 365);
}
}
function clearData() {
if (type == 'session') {
createCookie(getSessionName(), '', 365);
} else {
createCookie('localStorage', '', 365);
}
}
function getData() {
// Get cookie data.
var data = type == 'session' ? readCookie(getSessionName()) : readCookie('localStorage');
// If we have some data decode, parse and return it.
return data ? JSON.parse(decodeURIComponent(data)) : {};
}
function getSessionName() {
// If there is no name for this window, set one.
// To ensure it's unquie use the current timestamp.
if(!window.name) {
window.name = new Date().getTime();
}
return 'sessionStorage' + window.name;
}
// Initialise if there's already data.
var data = getData();
return {
length: 0,
clear: function () {
data = {};
this.length = 0;
clearData();
},
getItem: function (key) {
return data[key] === undefined ? null : data[key];
},
key: function (i) {
// not perfect, but works
var ctr = 0;
for (var k in data) {
if (ctr == i) return k;
else ctr++;
}
return null;
},
removeItem: function (key) {
delete data[key];
this.length--;
setData(data);
},
setItem: function (key, value) {
data[key] = value+''; // forces the value to a string
this.length++;
setData(data);
}
};
};
// Replace window.localStorage and window.sessionStorage with out custom
// implementation.
var localStorage = new Storage('local');
var sessionStorage = new Storage('session');
window.localStorage = localStorage;
window.sessionStorage = sessionStorage;
// For Safari private browsing need to also set the proto value.
window.localStorage.__proto__ = localStorage;
window.sessionStorage.__proto__ = sessionStorage;
})();
}
@s-efim

s-efim commented Feb 9, 2017

Copy link
Copy Markdown

Nice! This method is working correctly in Safari Private Mode. Thanks

@yavuzKomecoglu

Copy link
Copy Markdown

you rock!,..... you're the man! Thanks.

@orion110217

Copy link
Copy Markdown

I created a working one based on this.

@salmawisoky

Copy link
Copy Markdown

Immerse yourself in the challenging journey of Drift Boss, where you will drive through sharp turns and dangerous slopes.

@laradario09

Copy link
Copy Markdown

The mod menu example shows how enabling the right features and permissions in a browser can make content load correctly without errors.

@ffmi43

ffmi43 commented Mar 5, 2026

Copy link
Copy Markdown

Same here. The FFMI model shows how encouraging the right elements and licenses in a browser can make content load perfectly without errors.

@laradario

Copy link
Copy Markdown

When I need a quick break from coding, I play Traffic Rider MOD APK Latest Version
— it’s a motorbike racing game where you ride through traffic, complete missions, and improve your bike. It’s simple to jump into but keeps you focused with fast-paced gameplay.

@laradario

Copy link
Copy Markdown

When working with systems that depend on input and stored values, structure becomes really important. For example, a destiny matrix chart free tool takes a single input like a birth date and generates multiple connected outputs in a clear format, which is similar to how structured data handling improves reliability in code logic.

@danielthompson2650-ctrl

Copy link
Copy Markdown

Great insight. Storage polyfills like this are essential for improving browser compatibility especially when dealing with Safari private mode limitations. Solutions like these help developers build more reliable and user friendly applications across different environments. This is also useful for modern streaming and entertainment platforms such as TV APP projects where consistent storage behavior matters a lot.

@storygrc-eng

Copy link
Copy Markdown

This warning is very important for developers because browser compatibility can greatly affect user experience and website performance. A polyfill that fails in many browsers may cause features to break or behave unexpectedly, especially on older devices. It’s always a good idea to test compatibility carefully and provide stable alternatives before offering any tool or software for free download to users.

@jameijon

jameijon commented Jun 11, 2026

Copy link
Copy Markdown

Seven Arrows Recovery Arizona provides professional support and resources for individuals seeking help with alcohol addiction. Their programs focus on recovery guidance, behavioral health support, and helping people take steps toward a healthier life. Learn more about alcohol rehab Arizona and available treatment options.

@danielrobertand05-lab

Copy link
Copy Markdown

Great article with detailed insights and valuable statistics. Just like success in a racing game depends on strategy and performance, understanding development success rates helps researchers make smarter decisions. The data presented here is informative, well-structured, and highly useful for anyone interested in therapeutic innovation.

@superbearadvmod

Copy link
Copy Markdown

The mod example illustrates how properly configured browser settings and the required permissions can improve compatibility and ensure content runs smoothly without interruptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment