我有一个页面做了相当多的工作,我不希望用户能够离开该页面(关闭浏览器,回击按钮等)而不会收到警告.我发现onbeforeunload事件(我认为是特定于IE的,对我来说很好,因为项目使用了大量的ActiveX)非常有用.
问题是,我希望用户能够点击右上角的一个小"帮助"图标,随时弹出一个帮助窗口.这会导致onbeforeunload触发,即使主窗口永远不会到达任何地方并且页面永远不会卸载.
运行onbeforeunload事件时运行的JavaScript函数只将文本放入event.returnValue.如果我能够以某种方式确定帮助图标是被点击的图标那么我就不能在这种情况下将文本放入event.returnValue.但是我怎么能得到那个页面图呢?
让我猜一下:帮助"图标"实际上是一个与javascript:网址的链接?将其更改为真实按钮,真实链接,或者至少将功能放在onclick事件处理程序中(这可以防止默认行为).问题解决了.
<!-- clicking this link will do nothing. No onbeforeunload handler triggered.
Nothing.
And you could put something in before the return false bit...
...and the onunload handler would still not get called... -->
<a href="http://www.google.com/" onclick="return false;">blah1</a>
<!-- this should also do nothing, but IE will trigger the onbeforeunload
handler -->
<a href="javascript:void(0)">blah2</a>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7498 次 |
| 最近记录: |