0

I have a link to open a pdf in a browser. But, It gets focused on the new tab (the tab with the pdf. How to prevent this behavior?

This is the link:

<strong><a style="text-decoration:none" class='row_link' href='javascript:void(0)' target="_blank" onFocus="false">Open File</a></strong>

This is the jquery:

$("#myTable tbody").on('click','.row_link', function(e) {
    var no_s    = $(this).find('.filename').val().replace(/\//g , '');
    var b_url   = $('#base_url').val()+"assets/uploads/file/";

    var url     = b_url + no_s;
    window.open(url);
});

I've tried adding onfocus="false" in the anchor. But, It's no effect

ashura91
  • 441
  • 4
  • 17
  • You cannot do this on the browser. For reference: http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript – Rex Apr 12 '17 at 03:16

4 Answers4

1

<html>
<strong><a style="text-decoration:none" class='row_link' href='https://github.com/caiyongji' onclick="window.open('#','_blank');window.open(this.href,'_self');">Open File</a></strong>
</html>
Cai Yongji
  • 349
  • 1
  • 8
0

This effect is called the "pop under".

Basically you need to use window.blur() on the newly opened window to lose the focus and then refocus on your existing tab.

$("#myTable tbody").on('click','.row_link', function(e) {
var no_s    = $(this).find('.filename').val().replace(/\//g , '');
var b_url   = $('#base_url').val()+"assets/uploads/file/";

var url     = b_url + no_s;
var newWindow = window.open(url);
newWindow.blur();
window.focus();
});

Relevant Stackoverflow post

Community
  • 1
  • 1
Seth
  • 431
  • 1
  • 5
  • 9
0

Since you want the behaviour of opening background tabs, try the following code which is an example from this answer

For example in your jquery;

$("#myTable tbody").on('click','.row_link', function(e) {
    var no_s    = $(this).find('.filename').val().replace(/\//g , '');
    var b_url   = $('#base_url').val()+"assets/uploads/file/";

    var url = document.createElement("a");
    url.href = b_url + no_s;
    var evt = document.createEvent("MouseEvents");
    //the tenth parameter of initMouseEvent sets ctrl key
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                            true, false, false, false, 0, null);
    a.dispatchEvent(evt);
});

Let me know if it helps or if you find a problem

Community
  • 1
  • 1
Douglas Hosea
  • 1,002
  • 13
  • 30
0

If I understand correctly, what you need is, you don't want to show the newly opened tab to the user. This cannot be done as @Rex suggested, but what can be done as an alternative is, you can open a popup window and minimise it as soon as you want..

Please have a look on below code :

Parent Page

<a href="javascript:openPopUP();">Click to open popup</a>

Child Page

      <body onLoad="setTimeout('window.blur()', 1000);" 
      onFocus="setTimeout('window.blur()', 1000);">

You can change the time 1000 to what you want.

You can use this on child to keep focus on parent page

window.parent.opener.focus(); 
Vikash Mishra
  • 349
  • 3
  • 18
  • I do not want to open any window. Beacuse, User may open multiple pdf. If each pdf is openend in a new window, user may get lag on ther computer – ashura91 Apr 12 '17 at 03:29