Ryley38 Ryley38 - 11 days ago 11
Javascript Question

Using javascript to open new tab and reload parent page

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

Answer

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 Not 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.