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

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

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 1

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.

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>
发布评论

评论列表(0)

  1. 暂无评论