简体   繁体   English

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

[英]how to capture open link in new tab or window using jquery?

Is it possible to capture the right click open in new window/tab or mouse wheel open in new window/tab event using jQuery? 是否可以使用jQuery捕获在新窗口/选项卡中打开的右键单击或在新窗口/选项卡事件中打开鼠标滚轮?
UPDATE 1 更新1
Here is why I need it. 这就是我需要它的原因。 I have codeigniter application which uses pagination class. 我有使用分页类的codeigniter应用程序。 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. 分页链接已与使用AJAX加载容器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 更新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. 我刚刚意识到我的所有AJAX请求都由一个CI控制器提供服务,它实际上充当其他类/库的代理。 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. 在这个控制器中,我可以查看请求,如果它不是AJAX请求,我可以将用户重定向到另一个页面。

A workaround solution is to replace all applicable <a> elements with buttons, where (obviously) the buttons would call JavaScript that does the appropriate navigation. 解决方法是使用按钮替换所有适用的<a>元素,其中(显然)按钮会调用执行相应导航的JavaScript。

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. 如果你真的很热衷,你可以应用CSS来使按钮看起来像<a>元素,虽然我不推荐它,因为它会混淆那些可能试图将它们视为标准链接并右键或中间点击它们的用户。

(You could even get it to work for users that don't have JavaScript enabled by, eg, making each button a submit button in its own little form.) (您甚至可以让它为没有启用JavaScript的用户工作,例如,使每个按钮成为自己的小形式的提交按钮。)

At the very least you can catch a right-click, using .mousedown() (or, presumably, mouseup() ). 至少你可以使用.mousedown() (或者,可能是mouseup() )捕获右键单击。 See this StackOverflow answer about right clicks for more. 有关更多信息,请参阅StackOverflow关于右键单击的答案 And by catching it, you should be able to do a standard event.preventDefault() and then do as you like from there. 通过捕获它,你应该能够做一个标准的event.preventDefault() ,然后从那里做你喜欢的事情。 That may be overkill, however, as it could prevent the user from doing other things you want to allow them to do. 然而,这可能是一种矫枉过正,因为它可能会阻止用户做他们想让他们做的其他事情。

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. 假设您在新窗口中从页面“A”打开页面“B”。

If you want to check the page "B" is opened in a new window from page "A", then follow the below steps.. 如果要检查页面“B”是否在页面“A”的新窗口中打开,请按照以下步骤操作。

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

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. 如果您不希望人们在禁用JS时以常规方式或后备方式访问链接,那么它不应该是链接。 Just use any element you like (span, div, button, whatever you like) and style it like a link. 只需使用您喜欢的任何元素(span,div,button,无论您喜欢什么),并将其设置为链接样式。 Then bind the action using JS. 然后使用JS绑定动作。 Or you can use a link with href="#" or href="javascript: void(0)" . 或者您可以使用带有href="#"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. 这样,如果用户右键单击它并选择在新窗口中打开,那么它们将最终位于之前的同一页面中。

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

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