2012-08-07 46 views
0

如何點擊鏈接在新窗口中打開PDF,同時更改父窗口?如何點擊鏈接在新窗口中打開PDF,同時更改父窗口?

有點像? ...

<a href="assets/pdf/a_pdf_doc.pdf" target="new" 
    onclick="window.parent.location.href='newpage.html';">A PDF Doc</a> 

當然,上述不起作用....只是我認爲接近答案的一個例子。謝謝!

+0

你需要一些JavaScript - 比你的例子中已經有的更多。你有沒有聽說過'window.open()'? – 2012-08-07 14:47:32

回答

1

您可以將「新窗口」和「重定向」功能移到單個JS函數中。下面是定義可能是什麼樣子:

<script type="text/javascript"> 

    function openPdf(e, path, redirect) { 
     // stop the browser from going to the href 
     e = e || window.event; // for IE 
     e.preventDefault(); 

     // launch a new window with your PDF 
     window.open(path, 'somename', ... /* options */); 

     // redirect current page to new location 
     window.location = redirect; 
    } 

</script> 

然後在你的HTML:

<a href="assets/pdf/a_pdf_doc.pdf" 
    onclick="openPdf(event, 'assets/pdf/a_pdf_doc.pdf', 'newpage.html');"> 
    A PDF Doc 
</a> 

我會繼續的情況下,指定的正href屬性的用戶已經JS關閉。

+0

完美!像魅力一樣工作!感謝Cory! – 2012-08-07 16:12:32

0

嘗試改變鏈接:

<a href="javascript:void(0)" onclick="opentwowindows()">click me</a> 

,然後創建一個JavaScript函數當前頁面和父頁面;例如:

<script> 

function opentwowindows() { 
     window.parent.location.href='newpage.html'; 
     window.location.href="/anotherpage.html'; 
} 

</script>