2017-04-12 174 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