• 0

How do I open the SAME page on a NEW window, and cross JavaScript with them


Question

Hi people. I know this is an unusual question.

But I'm looking to open the same page on a new window. And with that new window, I would like to have cross javascript manipulation. Meaning that the new Window can manipulate the old window, and vice versa; From JavaScript to the complete DOM. Plus synchronous scrolling

Thanks a lot!

4 answers to this question

Recommended Posts

  • 0

Ok. Finally found the true answer to all of my headaches.

I'll explain what is the best way to communicate between a parent window and a child window from the same page. This logic can be tweaked for different pages (but from the same host).

Used: JavaScript + PHP


<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
<!--
var window_ = null;
var n = false; //Variable n


<?php
if(isset($_GET['n'])) //This will make the n variable go true if
//'?n' (without quotes) is found in the URL. Meaning, that the child exists
{
print('n = true;');
}
?>


function openChildWindow() //function which opens the child window.
{
if (window_ != null) //This will check if the window is opened
{
if ( window_.closed == false ) //if the window is opened, a.k.a is not closed
window_.close(); // close the window
window_ = null; // and nullify the variable, so it can be reopened if desired.
}

else
{
window_ = window.open(window.location.href+'?n'); //This is the tricky part.

/*Assign the window.location.href so it will open a new window with the same page
BUT assign it the n variable, so the GET function will receive it, and set n to true.
*/

window_.focus();
}
// Need to call on a delay to allow
// the child window to fully load...

}

function callChildWindowFunction()
{
if ( (window_ != null) && (window_.closed == false) )
{
window_.shout('bearl');
}


}
// -->

function shout(val)
{
alert(val);
}
$(document).ready(function(e) {
/*Wait for the document to load, so you can check if the variable n is true,
which means that **IT IS** the child window and it has been loaded completely. Meaning
that you can finally manipulate the child window with the parent document*/
if(n == true)
{
window.opener.callChildWindowFunction(); //Call the parent telling it "I'm ready"
}
});

</script>


<input type="button" value="Blear" onClick="openChildWindow();">[/CODE]

Now, explaining the code.

The trick here is to create an "artificial switch" which will tell the browser which page is the parent, and which is the child.

The one who does this, is the variable ***n***. By assigning it a false value, I'm telling it is the parent who has been loaded and not the child.

When the child window is opened, (please see the openChildWindow() function), it will verify if the window has been opened so it can close/open it, according to the situation.

If the window is opened, it will assign the EXACT URL plus the ?n variable which will help PHP to identify that it is the child window.

At the bottom of the opened page, after the document has been loaded, n will be true, so it will call the parent through the **window.opener** function. This will trigger a function, that will trigger the child's function (shout(val)).

You can also do this with two different pages.

Create a Page "A.html" and a Page "B.html" (Without quotes).

Page A will get the following code:

[CODE]
<script type="text/javascript">
<!--
var window_ = null;
function openChildWindow()
{
if ( (window_ != null))
{
if ( window_.closed == false )
window_.close();
window_ = null;
}

var windowUrl = 'B.html';

window_ = window.open(windowUrl);

window_.focus();
}

function callChildWindowFunction()
{
if ( (window_ != null) && (window_.closed == false) )
{
window_.shout('bearl');
}
}
// -->
</script>


<input type="button" value="Blear" onClick="openChildWindow();">[/CODE]

Page B will get this

[CODE]
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function shout(val)
{
alert(val);
}
$(document).ready(function(e) {
{
window.opener.callChildWindowFunction();
}
});

</script>[/CODE]

Note that the mechanics are the same. The only thing that there is no need for an "artificial switch" because the pages are different.

Just assign the page B you want to create a $(document).ready() function to verify it has loaded completely, and call the parent through the window.opener function so it can start calling the child.

If you've found a better way to do this, then go ahead and show me. setTimeOut, setInterval functions did not work because the code pre-executed before the time was out (Opera). Furthermore, this will assure you that the document was 100% loaded, and the code will not break if the user has slowpoke Internet connection.

This topic is now closed to further replies.
  • Posts

    • TechPowerUp GPU-Z 2.70.0 by Razvan Serea GPU-Z is a lightweight system utility designed to provide vital information about your video card and graphics processor. At launch, it automatically scans your system and reports the card name, GPU, release date and transistors, BIOS version, ROPs, memory type, and memory size. Main Features: Supports NVIDIA, AMD, ATI and Intel graphics devices Displays adapter, GPU and display information Displays overclock, default clocks and 3D clocks (if available) Includes a GPU load test to verify PCI-Express lane configuration Validation of results GPU-Z can create a backup of your graphics card BIOS No installation required, optional installer is available Support for Windows XP / Vista / Windows 7 / Windows 8 / Windows 10 (both 32 and 64 bit versions are supported) GPU-Z 2.70.0 changelog: Improved kernel driver security Added die size for Qualcomm Adreno 741 Added support for NVIDIA RTX 6000D, RTX Pro 500 Blackwell Embedded, Tesla V100-DGXS-32GB, PG500-216 Added support for Intel Arc Pro B70, B65, A60 ES, Alder Lake ES Added support for Qualcomm Snapdragon X2 Elite, 778G/782G Added vendor detection for HKC/Sambada, AWES Download page: GPU-Z 2.70.0 | 11.1 MB (Freeware) View: GPU-Z Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I know I won't ever be using it to make my game. I'd rather pay real humans.
    • Nah. For every indie dev that needs to create code for "stuff" or textures, it's a godsend enabler to possibly tackle a project that you may not otherwise. The end result and testing will tell the truth if everything works or doesn't, or a game is just mediocre slop, but now these tools are now there and it's the developer's duty to judge the outcome, and even more so for pro studios. And you gotta remember that they will be at an early stage.
    • whoosh my comment went over your head. Enjoy your notchless 3:2 OLED device
    • As a game dev, today was really depressing. They announced that Blueprints will be deprecated in UE6 to be fully replaced by their sh**ty Python-like Verse language... They also announced that 5.8 will be the last version of UE5 until UE6 comes out in MID 2029!!!! They have completely lost the plot.
  • Recent Achievements

    • One Month Later
      Vincian earned a badge
      One Month Later
    • First Post
      Jocimo earned a badge
      First Post
    • Week One Done
      suprememobiles48 earned a badge
      Week One Done
    • One Month Later
      Windows Guy earned a badge
      One Month Later
    • One Month Later
      Prasann earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      159
    3. 3
      PsYcHoKiLLa
      86
    4. 4
      Steven P.
      67
    5. 5
      neufuse
      63
  • Tell a friend

    Love Neowin? Tell a friend!