开发者

cookie that only exists while viewing a particular page?

The title ways it all. I want to use a cookie to send some metadata over to the client that is specific to the current page he is viewing. I'd rather not put it in the HTML, mainly because the metadata is only calculated after all the HTML is already generated and the closing </html> tag is in place.

Previously I was simply sticking it in a hidden <input/> after the final </html>, and browsers seem to render it fine, but I want to do the same thing while having standards compliant HTML. Although I want the cookie to be sent back whenever the client makes an ajax call to the server, I want it to invalidate immediately up开发者_C百科on leaving the page.


If the page is in an unique path (or can be URL-rewritten as such), then just set the cookie's path attribute to the page's full path. The browser will send the cookie only back whenever the page URL is covered by the cookie's path.

Alternatively, depending on the concrete functional requirement, you could also consider using the HTML5 data attributes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜