最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress - Using javascript to open new tab and reload parent page - Stack Overflow

programmeradmin0浏览0评论

I have a wordpress theme with a logo linking to a .pdf document. In order to link the logo to the document, I must use a custom javascript :

document.getElementById('logo').href = "javascript:window.open('mylink')"

I want this document open in a new tab. It currently works but for some reasons, I need to reload the parent page after the new tab has opened.

What can I add in order reload the parent page ? Thanks

I have a wordpress theme with a logo linking to a .pdf document. In order to link the logo to the document, I must use a custom javascript :

document.getElementById('logo').href = "javascript:window.open('mylink')"

I want this document open in a new tab. It currently works but for some reasons, I need to reload the parent page after the new tab has opened.

What can I add in order reload the parent page ? Thanks

Share Improve this question asked Nov 25, 2016 at 9:44 Ryley38Ryley38 3651 gold badge5 silver badges17 bronze badges 3
  • So you are saying if you click on logo, it opens a new tab, but then if you click again, it does not work? – Rajesh Commented Nov 25, 2016 at 9:46
  • What reasons do you need to reload the page for? – Reinstate Monica Cellio Commented Nov 25, 2016 at 9:48
  • @Rajesh Thanks for your answer. No, actually the parent page goes blank just after the new tab has opened so I need to reload it. I don't know if there is a simpler solution but ;location.reload()" do the job. – Ryley38 Commented Nov 25, 2016 at 10:26
Add a ment  | 

3 Answers 3

Reset to default 3
<a onclick="open_in_new_tab_and_reload('./path_to_pdf.pdf')" href="#">PDF</a>

<script>
function open_in_new_tab_and_reload(url)
{
  //Open in new tab
  window.open(url, '_blank');
  //focus to thet window
  window.focus();
  //reload current page
  location.reload();
}
</script>

To do exactly what you've asked you can make a small simple change...

document.getElementById('logo').href = "javascript:window.open('mylink');location.reload()";

However, without knowing why you need to refresh the page I can't say this is the best course of action. We need more information to be able to help you more appropriately.

Edit Now that you've explained why you need to reload the page (it goes blank when you click the link), you should change your previous code to this, in order to stop that happening...

var link = document.getElementById('logo');
link.href = "url_of_the_pdf";
link.target = "_blank";

Previously the output from window.open() was being written to the page, which caused the current page contents to be deleted. This method changes the link so that it works correctly, and should resolve your previous problem.

You don't need javascript for that. You can wrap the logo in a <a href="yourlink"></a> tag. Then, use the target attribute to tell it to open in a new tab.

<a target="_blank" href="yourlink"><img src="yourlogo"...></img></a>
发布评论

评论列表(0)

  1. 暂无评论