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
3 Answers
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>