Ron Wright
Ron Wright

Reputation: 3

How to click on a link to open a PDF in a new window while at the same time changing the parent window?

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!

Upvotes: 0

Views: 22918

Answers (2)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107606

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

Upvotes: 1

Losbear
Losbear

Reputation: 3314

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>

Upvotes: 0

Related Questions