Response.redirect kills local storage?

耗尽温柔 提交于 2021-01-27 03:51:54

问题


I am trying to work with HTML5 local storage in asp. I can read and write to the storage, but if I do a response.redirect the entire local storage is wiped out?

<script type='text/javascript'>

localStorage["email"] = "<%=email%>";

localStorage["remember"] = "1";
</script>

This works fine for saving and I can see the variable saved in local storage using Developer Tools.

However if after that I add

 response.redirect ("index.asp")

then the entire local storage is cleaned. How can I cause to to persist?


回答1:


The problem is (as Neil suggests) that localStorage takes a few milliseconds to execute and that you're redirecting before the process is complete. I had a similar problem with a javascript redirect after setting something in localStorage. You're using ASP so (and I can't say for sure without seeing more of the code) but if I remember correctly ASP is parsed on the Server so you're redirecting before any javascript executes.

Try using this instead:

<script type='text/javascript'>

localStorage["email"] = "<%=email%>";

localStorage["remember"] = "1";

setTimeout(function(){
   location.href = "index.asp";
},100)



来源:https://stackoverflow.com/questions/12489999/response-redirect-kills-local-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!