سؤال

Can I insert data into Web Storage from the server, like I do it with cookies by setting a header. By Web Storage I mean HTML5 localeStorage and sessionStorage.

هل كانت مفيدة؟

المحلول

Of course. But your server need to inject the code into the page it is serving.

From server this code need to be injected somewhere to the body or an extra javascript file.

<script type="text/javascript">
    localStorage[key] = myValue;
</script>

نصائح أخرى

Using C# as an example;

ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script>  
if(typeof(Storage)!=="undefined")
{
// Yes! localStorage and sessionStorage support!
// Some code.....
}
else
{
// Sorry! No web storage support..
};</script>", true);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top