2017-04-12 169 views
0

我有一个链接可以在浏览器中打开pdf。但是,它被集中在新标签(与pdf标签上如何防止这种行为如何在打开新文件时打开文件(打开文件)?

这是链接:?

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

这是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); 
}); 

我已经尝试在主播中添加onfocus="false"但是,这不起作用

+0

上你不能这样做的浏览器。作为参考:http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript – Rex

回答

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>

+0

它仍然专注于pdf标签 – ashura91

+0

现在试试这个。有用。不要在此页面中运行,复制并执行它。 –

+0

新选项卡未打开pdf。它会在新标签页中打开前一页。 – ashura91

0

这种效果被称为“弹出下”。

基本上你需要在新打开的窗口上使用window.blur()来失去焦点,然后重新关注你现有的选项卡。

$("#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

+0

它仍然专注于pdf标签 – ashura91

0

既然你要打开背景选项卡的行为,请尝试以下的代码是从这个answer

例如,在您的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); 
}); 

或者让我知道,如果它帮助,如果你发现一个问题

+0

它仍然专注于pdf标签。 – ashura91

+0

@ ashura91,看看我编辑的答案。 –

+0

现在pdf未能打开。 – ashura91

0

如果我理解正确的话,你需要的是,你不希望显示新开启的分页给用户。这不能做的@Rex建议,但什么是可以做到的替代方案是,你可以打开一个弹出窗口,只要你想减少它..

请对下面的代码来看看:

父页

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

子页面

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

您可以将时间1000更改为您想要的。

您可以使用该儿童保持专注父页面

window.parent.opener.focus(); 
+0

我不想打开任何窗口。Beacuse,用户可以打开多个PDF。如果每个pdf在新窗口中打开,则用户可能在计算机上滞后 – ashura91