How to click on a link to open a PDF in a new window while at the same time changing the parent window?
Something like? …
<a href="assets/pdf/a_pdf_doc.pdf" target="new"
onclick="window.parent.location.href='newpage.html';">A PDF Doc</a>
Of course the above doesn't work....just an example of what I perceive to be close to the answer. Thanks!
How to click on a link to open a PDF in a new window while at the same time changing the parent window?
Something like? …
<a href="assets/pdf/a_pdf_doc.pdf" target="new"
onclick="window.parent.location.href='newpage.html';">A PDF Doc</a>
Of course the above doesn't work....just an example of what I perceive to be close to the answer. Thanks!
Share Improve this question edited Aug 7, 2012 at 14:45 Cᴏʀʏ 108k20 gold badges168 silver badges198 bronze badges asked Aug 7, 2012 at 14:42 Ron WrightRon Wright 31 gold badge1 silver badge2 bronze badges 1-
You'll need some JavaScript for this -- more than you have already in your example. Have you ever heard of
window.open()
? – Cᴏʀʏ Commented Aug 7, 2012 at 14:47
2 Answers
Reset to default 1You could move the "new window" and the "redirect" functionality into a single JS function. Here's what that definition might look like:
<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>
And then in your HTML:
<a href="assets/pdf/a_pdf_doc.pdf"
onclick="openPdf(event, 'assets/pdf/a_pdf_doc.pdf', 'newpage.html');">
A PDF Doc
</a>
I would keep the regular href
attribute specified in case a user has JS turned off.
try changing the link to:
<a href="javascript:void(0)" onclick="opentwowindows()">click me</a>
and then create a javascript function the current page and the parent page; something like:
<script>
function opentwowindows() {
window.parent.location.href='newpage.html';
window.location.href="/anotherpage.html';
}
</script>