开发者

Global variable life in Javascript - destroyed on a reload?

(I am learning Javascript)

Problem: A page I am working on has 2 views, a data entry view with a textbox and a data rendering view. There is a a href src="currentpage#" link that switches between the 2 views. In order to transmit data from view to view, the javascript parses it from the current HTML and pushes it into the correct form for the other v开发者_开发知识库iew. This is ugly and I want to refactor it out, ideally into some sort of global where it can be neatly rendered down into the view on command (I'm refactoring the code base to a point where I can AJAX it from a server).

However, I am not certain about scoping rules and variable life in JS.


(if I understand your question correctly)

If you're just using internal links (a href='#myanchor' for example) your javascript variable will not go out of scope because your page won't actually get reloaded.

Just don't link to a href='currentpage.html#myanchor' as that will get your page reloaded.

Note that normal (level) scoping rules do still apply!


You can use cookies, or hide the variable in an higher iframe (IMHO better idea, since cookies are sent back and forth between the server and client).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜