简体   繁体   English

用户在JavaScript中离开之前的HTML页面后会怎样?

[英]What happens with previous HTML page after users navigate away from it in JavaScript?

Assumes I have two HTML pages, let call them page1.html and page2.html. 假设我有两个HTML页面,将它们称为page1.html和page2.html。 And then users navigate from page1.html to page2.html by made a call to assign or replace method of location object. 然后,用户通过调用分配或替换位置对象的方法从page1.html导航到page2.html。 I have some questions as below: 我有一些问题,如下所示:

Q1: What happens with the DOM tree of the page1.html, will it be unloaded and reloads latter when we navigate back to it from the page2.html? 问题1:page1.html的DOM树会发生什么,当我们从page2.html导航回它时,它将被卸载并重新加载吗? Can I reference DOM tree of the page1.html from page2.html? 我可以从page2.html引用page1.html的DOM树吗?

Q2: If I'm maintaining a socket connection in a socket object in the page1.html, can I pass that socket object to page2.html? Q2:如果我要在page1.html的套接字对象中维护套接字连接,是否可以将该套接字对象传递给page2.html? Can I listen for events of that socket object in page2.html? 我可以在page2.html中监听该套接字对象的事件吗?

Q3: Can I have a common object of both page1.html and page2.html. 问题3:我可以同时拥有page1.html和page2.html的共同对象。 And I can do something like maintaining a timer to do common task for both page1.html and page2.html in that object? 我可以做一些事情,例如维护计时器以对该对象中的page1.html和page2.html都执行常见任务? This object is like singleton object in other OOP language. 该对象类似于其他OOP语言中的单例对象。

Q1: What happens with the DOM tree of the page1.html, will it be unloaded and reloads latter when we navigate back to it from the page2.html? 问题1:page1.html的DOM树会发生什么,当我们从page2.html导航回它时,它将被卸载并重新加载吗?

More or less. 或多或少。 Browsers cache previously visited pages and probably won't fire a new load event when revisited using "back" navigation. 浏览器缓存以前访问过的页面,并且使用“后退”导航重新访问时可能不会触发新的加载事件。 Some will keep the page in its previous state, some wont. 有些将使页面保持其先前状态,有些则不会。

Can I reference DOM tree of the page1.html from page2.html? 我可以从page2.html引用page1.html的DOM树吗?

No. From a DOM point of view, page1.html has ceased to exist. 不会 。从DOM的角度来看, page1.html已经不复存在。

Q2: If I'm maintaining a socket connection in a socket object in the page1.html, can I pass that socket object to page2.html? Q2:如果我要在page1.html的套接字对象中维护套接字连接,是否可以将该套接字对象传递给page2.html?

No, but you may be able to serialise it when page1 closes, send it to the server, then include it in page2 where it can be restored ( cf JSON). 没有,但你可以在第1页关闭,以连载,将其发送到服务器,然后将其包含在第2页它可以恢复(CF JSON)。

Can I listen for events of that socket object in page2.html? 我可以在page2.html中监听该套接字对象的事件吗?

Not if it's still in page1 , but you may be able to emulate that behaviour on the server. 如果它仍在page1中 ,则不是这样,但是您可以在服务器上模拟该行为。

Q3: Can I have a common object of both page1.html and page2.html. 问题3:我可以同时拥有page1.html和page2.html的共同对象。

Not using javascript. 不使用JavaScript。 However, if you passed the object to page2 (say via serialisation or some other method via the server) then you can perhaps pass it back again. 但是,如果将对象传递给page2 (例如通过序列化或通过服务器的其他方法),则可以再次将其传递回。

And I can do something like maintaining a timer to do common task for both page1.html and page2.html in that object? 我可以做一些事情,例如维护计时器以对该对象中的page1.html和page2.html都执行常见任务?

No, but again you can do such stuff server side. 不,但是您也可以在服务器端进行此类操作。

This object is like singleton object in other OOP language. 该对象类似于其他OOP语言中的单例对象。

Cool. 凉。

If you describe what you are actually trying to do (ie the outcome you are trying to achieve rather then the method you are using to do it), a suitable architecture or design pattern might be suggested, eg as Abhi has intimated, a one page application may suit. 如果您描述了您实际想要做的事情(即要达到的结果,而不是要使用的实现方法),那么可能会建议一个合适的体系结构或设计模式,例如Abhi暗示的那样,一页应用程序可能适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当页面导航时,Javascript会发生什么? - What happens to Javascript when page navigates away? 如何在锚定href中利用javascript而不浏览页面 - how to utilize javascript in the anchor href and not navigate away from the page Javascript内存分析器:如果在页面离开后在堆快照中显示Htmldiv元素,那是什么意思? - Javascript memory analyser: What does it mean if a Htmldiv element shows up in the heapsnapshot after the page is navigated away from? 使用jQuery导航离开页面 - Use jQuery to navigate away from page 我可以建立一个链接1.离开页面浏览并执行2. Javascript吗? - Can I make a link 1. navigate away from page and 2. execute Javascript? 登录后,php导航至上一页 - php navigate to previous page after login workaround 导航到javascript中的html页面 - navigate to html page within javascript 从多个用户列表中选择他/她后,如何导航到特定用户的个人资料页面? - How to navigate to a specific users profile page after selecting him/her from a list of multiple users? 接收推送通知后,从javascript导航到ionic中的特定页面 - Navigate to particular page in ionic from javascript after reciving push notification 如何导航到 javascript 中的另一个 html 页面 - How to navigate to another html page in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM