0

Ruby/Rails Jquery

Forcing a link to open in a new tab by pressing ctrl + click returns a 404 page when the response is not HTML. I would like to be able to throw an alert that says "This link is not available in a new tab."

What is the best way to accomplish this globally? I have already updated the high-use cases where the action is controlled by a JQuery 'on click' event, but I am looking for a more robust solution.

Example: HTML:

 <a href="http://some_link/some_action">some link</a>

CONTROLLER:

 def some_action
   ...
   respond_to do |format|
     format.js
   end
 end
Peter Black
  • 1,142
  • 1
  • 11
  • 29
  • You're going to need to give at least a little bit of code here for context. Opening in a new window shouldn't cause an automatic 404 so long as your link isn't pure JavaScript. – tadman Apr 27 '16 at 20:23
  • Any response that is returned from the controller that is not HTML. For example JS or JSON. Im not sure what kind of code you are looking for? Its a link and the controller returns JS. – Peter Black Apr 27 '16 at 20:55
  • I have updated my question. – Peter Black Apr 27 '16 at 20:55
  • You may need to put in some sort of useful HTML version to capture the case where someone opens it in a new tab. Your controller is JavaScript only. – tadman Apr 27 '16 at 21:18
  • I was thinking that could be an option as well, unfortunately, some of those links are bootstrap modals and it would lose some of the look and feel of a regular application. – Peter Black Apr 27 '16 at 21:26
  • 1
    Unless you can prevent clicking and opening in a new tag, something that can be done by having no link but include a `click()` handler, you absolutely must produce something HTML-wise when it's clicked even if it's a "how did you get here" page. – tadman Apr 27 '16 at 21:35
  • I'm going to tinker with adding an instance variable to the controller that records all the params, overload the respond_to block with a generic index page and have some JS fire that reloads the content. Kinda hacky but I want that application "look and feel". – Peter Black Apr 27 '16 at 21:56

1 Answers1

2

There's a couple of options but what I would suggest is using something like this:

$('.js-non-tabbable-link').on('click', function(event) {
  if (event.metaKey || event.ctrlKey){
    console.log("NOPE");
    event.preventDefault();
  } 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class='js-non-tabbable-link'>Click Me</a>
Mitch
  • 239
  • 2
  • 8
  • This is probably the best answer. Unless anyone knows of a way to detect a control click from the controller action. – Peter Black Apr 27 '16 at 21:01
  • I will accept this answer if no one else comes up with anything by tomorrow. – Peter Black Apr 27 '16 at 21:58
  • I don't know that anything is injected into the HTTP Headers when you control click but you could always confirm with some debugging. – Mitch Apr 27 '16 at 22:16
  • @tadman, no it doesn't but I think this post addresses that (http://stackoverflow.com/questions/7554507/jquery-detect-mouse-click-and-open-target-in-new-tab) – Mitch Apr 27 '16 at 22:20