开发者

HTTP cookie between two HTML pages

开发者 https://www.devze.com 2023-02-19 11:41 出处:网络
I have two HTML pages. After entering few inputs users will be redirected from first page to second page. Before redirecting the user to second HTML page(using window.location=\"new HTML URL\"), I per

I have two HTML pages. After entering few inputs users will be redirected from first page to second page. Before redirecting the user to second HTML page(using window.location="new HTML URL"), I persist few of the user inputs in cookie using document.cookie DOM API.

When I am in the second HTML page, I could not retrieve the value from this cookie. I think since document object would have changed in the new HTML page, my cookie values become inaccessible.

Can someone tell me: how do I retrieve the value from a cookie persisted by one javascript in one HTML page in other HTML page i.e cookie writte开发者_C百科n by HTML A's javascript in HTML B's javascript?

I don't have any server-side code, so I could not take advantage of server-side logic. Also I am not supposed to pass the values in URL. So I need a solution on plain javascript and HTML.

If some one has a better solution please let me know. Thanks


try to use localStorage instead of cookies,

// set your values in the first page
localStorage.setItem('itemKey', 'values');

// on the second page, retrieve them
var values = localStorage.getItem('itemKey');

you can use a jStorage plugin for cross browser behaviour.

also refer to this question for storing objects instead of strings


JAAulde is on point with his answer.

For what the OP is trying to do something like PHP would be great, in that case I wouldn't bother with cookies in order to just pass data between two pages, that's just silly. However, if true persistence was needed and the data requirements were simple cookies would be the way to go even while using a language such as PHP.

Those are rather draconian constraints, is this a class project? That said there aren't any other ways to do what you're attempting, save for an ugly and highly insecure hack of the DOM.

0

精彩评论

暂无评论...
验证码 换一张
取 消