Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 513
ALG Avatar asked Aug 27 '12 14:08

ALG


People also ask

How do I switch between tabs in JavaScript?

Using Javascript, triggering an alert can have the desired effect. Run this code in your console, or add to your html file in one tab and switch to another tab in the same browser. setTimeout(function(){ alert("Switched tabs"); }, 5000); The alert appearing after the timeout will trigger tab switch.

Can JavaScript access other tabs?

You can access the new window/tab if it was opened with JavaScript and the page indeed is in the same domain. var win = window. open("/path_to_page"); Then you'll have to wait for the page to load before you can access e.g. the title.


2 Answers

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 accomplish (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.

like image 104
josh3736 Avatar answered Sep 19 '22 14:09

josh3736


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.

like image 33
Joe Avatar answered Sep 22 '22 14:09

Joe