简体   繁体   English

如何防止小程序跨 web 页面卸载?

[英]How to Prevent an Applet from Unloading across web pages?

I have a large applet that takes some 40 seconds to initialize (cabs are cached so ignore download time for this question).我有一个大的小程序,需要大约 40 秒来初始化(出租车被缓存,所以忽略这个问题的下载时间)。

The issue is, if the user goes to Page A which launches the applet (single window) and then navigates to page B, the applet is unloaded.问题是,如果用户转到启动小程序(单个窗口)的页面 A,然后导航到页面 B,小程序将被卸载。 If the user goes back to Page A, they incur the 40 seconds init time once again.如果用户返回到页面 A,他们将再次产生 40 秒的初始化时间。

I'd like to be able to launch the applet, incurring the 40 seconds init time once and only once.我希望能够启动小程序,只产生一次 40 秒的初始化时间。 The applet needs to reside inside a single browser window (the same window as my web application).小程序需要驻留在单个浏览器 window 中(与我的 web 应用程序相同的 window)。 In other words, I cannot launch the applet in a popup.换句话说,我无法在弹出窗口中启动小程序。

Does anyone have any creative ideas around how to prevent the applet unloading?有人对如何防止小程序卸载有任何创意吗?

You could show a warning, this way:您可以通过以下方式显示警告:

if(window.body)window.body.onbeforeunload=function(){if(window.event)window.event.returnValue="UNLOAD MSG";};else window.onbeforeunload=function(){return "UNLOAD MSG";};

This works in allmost every browser, but you cannot change the text surrounding your text.这适用于几乎所有浏览器,但您无法更改文本周围的文本。

The only option would be to put you content in an iframe that encompasses your whole document and use absolute positionning for the applet.唯一的选择是将您的内容放在包含整个文档的 iframe 中,并为小程序使用绝对定位。

<body>
 <iframe id='content' src='FIRST_URL'></iframe>
 <object id='applet'></object>
</body>

With a css like this one:使用像这样的 css:

#content {
  width:100%;
  height: 100%;
}

#applet {
  position: absolute;
  left: LEFT_POSITION;
  top: TOP_POSITION;
  width: DESIRED_WIDTH;
  height: DESIRED_HEIGHT;
}

There are MANY drawbacks:有很多缺点:

  • The applet will always be positionned at the same place, no matter the page无论页面如何,小程序都将始终定位在同一位置
  • You will suffer to get the iframe full width and full height, especially in IE您将很难获得 iframe 全宽和全高,尤其是在 IE 中
  • It's possible you will also suffer to have the applet showing properly (an object on top of an iframe, I can't imagine what troubles you'll run into)您也可能会遇到小程序正常显示的问题(object 位于 iframe 之上,我无法想象您会遇到什么麻烦)
  • The applet will stay if your user click to a link leading outside of your site unless you set a target for the link (ie, target='top' or target='__blank')如果您的用户单击指向您网站外部的链接,则小程序将保留,除非您为该链接设置目标(即 target='top' 或 target='__blank')

Put the Applet in a frame that doesn't unload, or prevent the user from going to another page in the web page that contains the Applet.将 Applet 放在不卸载的框架中,或阻止用户转到包含 Applet 的 web 页面中的另一个页面。

I thought maybe there'd be a way to popup a tiny browser window and have the applet load in that, then using Javascript somehow pull in the applet from the smaller window into the larger browser.我想也许有办法弹出一个微型浏览器 window 并在其中加载小程序,然后使用 Javascript 以某种方式将小程序从较小的 window 拉入较大的浏览器。

I don't think I can manipulate the DOM in this way though.我认为我不能以这种方式操作 DOM。

Any other ideas?还有其他想法吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM