1

I know that this is the default behavior of the browser (opening in a new tab), but are there any tricks to overcome them? Not using the other event handlers - such as onmousedown/onmouseup for <a>? Trying to open with: window.open(url);

It is a necessary to make it on the requirements specification.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Kseniya Yudina
  • 137
  • 1
  • 11
  • Why do you want to override the expected browser behaviour? – moopet Apr 02 '16 at 16:27
  • This must be done on the technical task. And it's driving me crazy for over two weeks, yes. – Kseniya Yudina Apr 02 '16 at 17:24
  • Unfortunately, the answer seems to be that Chrome doesn't allow JavaScript click handlers to open new windows. See [this question](http://stackoverflow.com/q/2572333/1016716). Gops AB's answer, if you correct the errors, still works only in Mozilla; ([fiddle](http://jsfiddle.net/MrLister/aeavd4L5/2/)). – Mr Lister Apr 09 '16 at 06:58

3 Answers3

0
    if (e.ctrlKey){
       window.open(url,'_blank')
     }

It will open the url in new tab. Check for ctrl key in the click event

   $(".yourLink").bind('click', function(e){
         e.prevenDefault();
         if (e.ctrlKey){
       window.open(url,'_blank');
     }
   });

EDIT

If you want to open in new window, specify width and height as follows

    window.open(url,'_blank', "height=255,width=255");
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • window.open(url, _blank", "width=700,height=620,") - open in new window only in IE. :( – Kseniya Yudina Apr 02 '16 at 16:36
  • My version of Chrome is 44.0.2403.157 m. I checked - it's don't work. – Kseniya Yudina Apr 02 '16 at 17:13
  • @GopsAB Your example doesn't work, because it contains errors. 1) `preventDefault` is misspelled 2) `preventDefault` should be in the `if` block, or clicking won't do anything at all, and 3) url isn't defined. – Mr Lister Apr 09 '16 at 06:48
  • @KseniyaYudina Your example has an error too, a missing quote. – Mr Lister Apr 09 '16 at 06:48
  • Thank you, Mr Lister. I solved this problem in another way. I change click event on onmouseup event. – Kseniya Yudina Apr 10 '16 at 10:53
  • And then - to separate these two events (onclick and onmouseup) - I checked the offsetLeft property of the button (event.currentTarget.offsetLeft == 0) //This applies for onmouseout event. – Kseniya Yudina Apr 10 '16 at 11:02
  • Note that this assume's holding the Ctrl key is what triggers links to open in a new window. That's how Windows works, but on macOS users hold the Command or "Meta" key to trigger this, because Ctrl+clicking already has a different meaning. So you'd need to check "e.metaKey" to give MacOS user's the same behavior. In general I wish there was a better solution that didn't bake in assumptions about key behavior. We need something like a "e.newWindowKey" or something. – Chris Mar 08 '22 at 00:26
0

Actually, it is related to the CTRL key, seems like a bug in Chrome and it takes it as default behaviour even the e.preventDefault() was called. In order to trick the browser you have to get out of the listener stack using a setTimeout function.

 $(".yourLink").bind('click', function(e){
    if (e.ctrlKey){
          var openWindow= function(){ 
              window.open(url,'name','width=' + screen.width + ',height=' + screen.height + ',location=yes,scrollbars=yes,status=yes;');
          }
          setTimeout(openWindow, 500); 
    }
 });
0

We have a custom javascript function in the "onclick" of an anchor tag in a product of ours. This because we want to add additional parameters to the URL when navigating to the next screen.

Anyway, we implemented "window.open()" while holding "Ctrl" to open a new tab. However, in Chrome, the javascript on the new tab was SO slow to execute and stayed that way the entire duration of that tab existing. The following solved the issue:

setTimeout(function() {window.open(url);}, 250);

I've adopted a montra: "when in doubt, setTimeout" in Chrome. For some reason it solves so many wonky things. Anyway, hope this helps!