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

Switching between the same two tabs only in a browser using javascript and html - Stack Overflow

programmeradmin3浏览0评论

My idea of what am trying to do is When I open a website on one tab of an internet explorer broweser and click on a link it should open a new tab in the same browser with a pdf page init ... the pdf page has a lot of links which if u try clicking on any of them they should take you back to the old tab where u managed to lunch open the pdf from and keep switching between those two tabs only I hope the idea is clear .. am trying to do this using html and javascript ... this what I wrote but am still missing a lot here. Thanks in advance for any help provided

this piece here lunches another instant in another window

<html>
<head>
<script>
function son()
{

 myWindow=window.open('','','width=500,height=500');
 myWindow.document.write(" SON !");
 myWindow.focus();
 myWindow.opener.document.write("<p> DAD !</p>");
}
</script>
</head>
<body>

<input type="button" value="Open " onclick="son()" />

</body>
</html>

This file is where I have the pdf file built in.

<object data="Test.pdf" type="application/pdf" width="100%" height="100%">

<p>It appears you don't have a PDF plugin for this browser.
you can <a href="Test.pdf">click here to
download the PDF file.</a></p>

</object>

thanks again

My idea of what am trying to do is When I open a website on one tab of an internet explorer broweser and click on a link it should open a new tab in the same browser with a pdf page init ... the pdf page has a lot of links which if u try clicking on any of them they should take you back to the old tab where u managed to lunch open the pdf from and keep switching between those two tabs only I hope the idea is clear .. am trying to do this using html and javascript ... this what I wrote but am still missing a lot here. Thanks in advance for any help provided

this piece here lunches another instant in another window

<html>
<head>
<script>
function son()
{

 myWindow=window.open('','','width=500,height=500');
 myWindow.document.write(" SON !");
 myWindow.focus();
 myWindow.opener.document.write("<p> DAD !</p>");
}
</script>
</head>
<body>

<input type="button" value="Open " onclick="son()" />

</body>
</html>

This file is where I have the pdf file built in.

<object data="Test.pdf" type="application/pdf" width="100%" height="100%">

<p>It appears you don't have a PDF plugin for this browser.
you can <a href="Test.pdf">click here to
download the PDF file.</a></p>

</object>

thanks again

Share Improve this question asked Aug 27, 2012 at 14:37 ALGALG 931 gold badge1 silver badge6 bronze badges 3
  • You can't force a switch to a different tab or window via JavaScript. – zzzzBov Commented Aug 27, 2012 at 14:45
  • If the opened page were a Web page, you could possibly do something like this with postMessage (but even that wouldn't switch tabs), but I suspect it will not be possible with a PDF. – apsillers Commented Aug 27, 2012 at 14:46
  • @zzzzBov even if I try putting the pdf in a iframe – ALG Commented Aug 27, 2012 at 14:51
Add a ment  | 

3 Answers 3

Reset to default 7

In the old days, you could use a window's focus method to bring a window/tab into the foreground. However, abuse (mostly in the form of advertisers' popup windows) has resulted in browsers restricting or disabling that functionality.

If we ignore the PDF part, conceptually, this is a very simple request. First, open a window and hold on to its reference:

var pop = window.open('page.html'); // opens in new tab on most browsers

In the secondary page, switching back to the original was simple:

window.opener.focus(); // no longer works in most modern browsers

And from the first page, to switch back:

pop.focus();  // might work

In my test, I couldn't get IE 9 or Chrome 21 to switch back to the opener tab. In Chrome, I could open the second page, manually switch back to the original tab, and calling pop.focus() did bring the second tab back in focus. (IE did nothing.) I was able to force Chrome back to the opening page by calling window.opener.alert('...'); from the secondary page, but that's an ugly hack.

So it looks like you won't be able to pull this off, at least not reliably.

I'm not sure exactly what you're trying to acplish (a TOC?), but have you thought about opening two windows? Open one with your links that covers the left-hand side of the screen, and another on the other half with the PDF.

JavaScript does not have APIs for controlling tabs. Therefore, you can't do it.

You can open windows, but you can't control if it will be a tab or window.

One alternative possibility involves NOT opening a second window or tab. If you open another/replacing page in the current window or tab, you can use the History object to switch between the two pages.

history.go(-1); //takes you back to previous page
history.go(1);  //takes you forward to the page from which you went back.
发布评论

评论列表(0)

  1. 暂无评论