如何使用 jQuery 捕获新选项卡或窗口中打开的链接?

发布于 2024-12-11 03:57:48 字数 377 浏览 0 评论 0原文

是否可以使用 jQuery 捕获右键单击在新窗口/选项卡中打开或鼠标滚轮在新窗口/选项卡中打开事件?
更新1
这就是我需要它的原因。我有使用分页类的 codeigniter 应用程序。我使用此类来显示网格。分页链接已与使用 AJAX 加载容器 div 中的下一页的方法绑定。现在,有人可以右键单击并在我不想要的新选项卡/窗口中打开下一页。恕我直言,处理此问题的唯一方法是如何捕获(右键单击或鼠标滚轮按钮单击)在新窗口/选项卡事件中打开。
更新2
我刚刚意识到我的所有 AJAX 请求都由一个 CI 控制器提供服务,该控制器实际上充当其他类/库的代理。在此控制器中,我可以查看请求,如果它不是 AJAX 请求,我可以将用户重定向到另一个页面。

Is it possible to capture the right click open in new window/tab or mouse wheel open in new window/tab event using jQuery?
UPDATE 1
Here is why I need it. I have codeigniter application which uses pagination class. I use this class to display a grid. The pagination links have been bind with a method that uses AJAX to load the next page in a container div. Now some one can right click and open the next page in new tab/window which I don't want. IMHO, the only way to handle this is to some how trap the (right click or mouse wheel button click) open in new window/tab event.
UPDATE 2
I just realised all my AJAX requests are being served by one CI controller which actually acts as a proxy to other classes/libs. In this controller I can look at the request and if it isn't an AJAX request I can redirect the user to another page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哎呦我呸! 2024-12-18 03:57:48

解决方案是将所有适用的 元素替换为按钮,其中(显然)按钮将调用执行适当导航的 JavaScript。

如果您真的很热衷,可以应用 CSS 使按钮看起来像 元素,但我不推荐这样做,因为它会让可能尝试将它们视为标准链接的用户感到困惑,并且右键或中键单击它们。

(您甚至可以让它为没有启用 JavaScript 的用户工作,例如,使每个按钮以其自己的小形式成为提交按钮。)

A workaround solution is to replace all applicable <a> elements with buttons, where (obviously) the buttons would call JavaScript that does the appropriate navigation.

If you're really keen you can apply CSS to make the buttons look like <a> elements, though I don't recommend it because it confuses users who might try to treat them as standard links and right- or middle-click them.

(You could even get it to work for users that don't have JavaScript enabled by, e.g., making each button a submit button in its own little form.)

还如梦归 2024-12-18 03:57:48

至少您可以使用 .mousedown() (或者大概是 mouseup())来捕获右键单击。有关详细信息,请参阅此 StackOverflow 答案。通过捕获它,您应该能够执行标准的 event.preventDefault() ,然后从那里按照您的意愿进行操作。然而,这可能有点矫枉过正,因为它可能会阻止用户执行您希望他们执行的其他操作。

At the very least you can catch a right-click, using .mousedown() (or, presumably, mouseup()). See this StackOverflow answer about right clicks for more. And by catching it, you should be able to do a standard event.preventDefault() and then do as you like from there. That may be overkill, however, as it could prevent the user from doing other things you want to allow them to do.

人间☆小暴躁 2024-12-18 03:57:48

我现在几乎修复了我正在处理的页面的类似问题。我的解决方法是在页面中进行一些更改(如果已在新窗口中打开该页面)...

假设您在新窗口中从页面“A”打开页面“B”。

如果您想检查页面“B”是否从页面“A”在新窗口中打开,请按照以下步骤操作。

If (document.referrer == "A" && window.history.length > 1) {
     alert("I am page 'B' and opened from page 'A' in a new window");
}

I almost fixed a similar issue now for a page which I am working on. My fix was to do some changes in the page if that has been opened in a new window....

Assume that you open a page "B" from page "A" in a new window.

If you want to check the page "B" is opened in a new window from page "A", then follow the below steps..

If (document.referrer == "A" && window.history.length > 1) {
     alert("I am page 'B' and opened from page 'A' in a new window");
}
放我走吧 2024-12-18 03:57:48

如果您不希望人们以通常的方式访问链接或在 JS 禁用时回退,那么它不应该是链接。只需使用您喜欢的任何元素(span、div、按钮,任何您喜欢的元素)并将其样式设置为链接即可。然后使用JS绑定动作。或者您可以使用带有 href="#"href="javascript: void(0)" 的链接。这样,如果用户右键单击它并选择在新窗口中打开,那么他们最终将进入之前的同一页面。

If you don't want people to access link the usual way or fallback when the JS is disabled, then it shouldn't be a link. Just use any element you like (span, div, button, whatever you like) and style it like a link. Then bind the action using JS. Or you can use a link with href="#" or href="javascript: void(0)". That way if users right click it and choose to open in a new window, then they will end up in the same page they were before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文