选项卡式浏览导致 Javascript 不一致?
在选项卡式浏览器(对我来说是 FF 3.5)中,我经常使用 CTRL 键单击或中键单击在新选项卡中打开链接。 当链接包含 Javascript 函数时,可以:
- 打开一个空白的新选项卡,并在地址栏中显示
javascript:window.print()
或“what-have-you”。 - 该函数成功执行并且打开一个空白的新选项卡。
事实是,这似乎并不是所有 Javascript 函数都会一致发生。 例如:
- 此页面,当按住 CTRL 键单击时,打开打印对话框并生成一个空的新选项卡。
- 白皮书链接位于此页面,当按住 CTRL 键单击时,仅生成一个空的新选项卡。
有问题的链接分别包含以下代码:
javascript:window.print();
javascript:_hbRedirect('PDF Downloads','White Paper','URL.pdf',' &c1=TITLE|US-en')
这个问题有点类似。 评价最高的答案详细介绍了基于 AJAX 的解决方案,但我找不到解释为什么某些 Javascript 函数在选项卡上的行为与其他函数不同。
In a tabbed browser (FF 3.5 in for me), I often open links in new tabs with CTRL-click or middle-click. When the link contains a Javascript function, either:
- A blank new tab opens with
javascript:window.print()
or what-have-you in the address bar. - The function executes successfully and a blank new tab opens.
Thing is, this doesn't seem to happen consistently for all Javascript functions. For example:
- The "Print" button on this page, when CTRL-clicked, opens a print dialogue and generates an empty new tab.
- The Whitepaper links on this page, when CTRL-clicked, only generates an empty new tab.
The links in question contain the following code, respectively:
javascript:window.print();
javascript:_hbRedirect('PDF Downloads','White Paper','URL.pdf','&c1=TITLE|US-en')
This SO question is somewhat similar. The best-rated answer details an AJAX-based solution, but I could not find an explanation for why some Javascript functions behave differently with tabs than others.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为
window.print()
已默认定义,因此可以正常调用。javascript:_hbRedirect('PDF Downloads','White Paper','URL.pdf','&c1=TITLE|US-en')
链接调用未定义的函数,因此不会发生任何情况。That happens because
window.print()
is already defined by default so it is called normally.The
javascript:_hbRedirect('PDF Downloads','White Paper','URL.pdf','&c1=TITLE|US-en')
link calls an undefined function so nothing happens.JavaScript 不应与 XHTML 混合。 让链接的 href 属性只是您要链接到的页面或文档。 如果您想使用 a 标签做一些特殊的事情,请使用事件注册。
A la MooTools:
JavaScript shouldn't be mixed with XHTML. Let the href attribute of a link just be the page or the document you want to link to. If you want to do something special with an a tag, use event registering.
A la MooTools: