• 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

    • yes AND no the "original" or plain/normal Optiplex 7010 won't be getting any more new firmware updates BUT the Optiplex SFF/SFF Plus {small form factor}, Micro/Micro Plus & Tower/Tower Plus 7010 editions DO get new updates such as this new one   and here are similar guides from the Dell web site for Dell systems: https://www.dell.com/support/kbdoc/en-us/000390990/secure-boot-transition-faq https://www.dell.com/support/kbdoc/en-us/000347876/microsoft-2011-secure-boot-certificate-expiration
    • AT&T has been spying on US citizens with the NSA for decades.. they just know how to keep it more under wraps.. the evil level is still there.
    • >Improved system sounds when using Windows in dark mode. The story behind that bug would be an interesting one.
    • Edifier S3000MKII hi-fi audiophile grade bookshelf speaker is at its lowest price now by Sayan Sen Yesterday we covered a bunch of Dolby Atmos soundbar deals with several ones from Sony, as well as from JBL, Samsung, Polk Audio, and more. You can check them out in this dedicated piece. Those are not audiophile category speakers though as they are built with home theater use in mind. If you are searching for the former then Edifier has its S3000MKII at its lowest price at the moment (purchase link under the specs table down below). This is a two-way bookshelf monitor speaker designed to produce accurate sound. While it may not produce the best high-fidelity audio possible out there, it should still be significantly better than what you will get on soundbars of this price range. As such it will do justice to high-res audio played back through it. The only thing that may feel lacking is sub-bass as Edifier claims the unit can go down to 38 Hz, which should be enough for studio monitor purposes, but not for deep room-shaking rumbling bass. Where this does excel though is in its treble reproduction. With its super-tweeter, it claims to go as high as 40 kHz in the frequency spectrum, which should offer a sense of "air"yness. This is an active speaker which means it packs its own amplfication. It has a top-notch Class D amp that may be able to rival many Class AB designs too in terms of sound reproduction quality. The technical specs of the Edifier S3000MKII are given in the table below: Specification Value RMS Output Power 256W RMS (Treble: 8W × 2, Mid-Low: 120W × 2) Tweeter Driver 107mm × 107mm Planar Magnetic Tweeter Mid-Low Driver 6.5-inch (179mm) Long-Throw Aluminum Diaphragm Driver Frequency Response 38Hz – 40kHz Signal-to-Noise Ratio ≥ 85dB (A) Bluetooth Version Bluetooth 5.0 Bluetooth Codec Qualcomm® aptX™ HD Wireless Speaker Link Proprietary 5.8GHz wireless connection between speakers Supported Hi-Res Audio Hi-Res Audio Certified, up to 24-bit/192kHz Digital Processing XMOS XU216 Digital Signal Processor Audio Inputs Balanced XLR, Optical, Coaxial, USB Type-B, Line In, Bluetooth Input Sensitivity (USB) 400 ± 50mFFs Input Sensitivity (Optical) 400 ± 50mFFs Input Sensitivity (Coaxial) 400 ± 50mFFs Input Sensitivity (Bluetooth) 450 ± 50mFFs Input Sensitivity (Balanced XLR) 1000 ± 50mV Input Sensitivity (Line In) 600 ± 50mV ADC Capability Up to 24-bit/192kHz DSP Capability Up to 24-bit/192kHz DIX Capability Up to 24-bit/216kHz DAC Capability Up to 32-bit/384kHz XMOS Processing Power Up to 2,000 MIPS Edifier S3000MKII Audiophile Active (Powered) Wireless Speakers: $799.99 (Sold by Edifier US, Shipped by Amazon US) If you do not have the kind of budget to spend on the S3000MKII, you can also check out the Edifier R1280Ts which is right now on sale at just $114 (its lowest price in a very long time). Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • One Year In
      bernmeister earned a badge
      One Year In
    • Week One Done
      Scoobystu earned a badge
      Week One Done
    • Week One Done
      tuben earned a badge
      Week One Done
    • First Post
      OffsetAbs earned a badge
      First Post
    • Reacting Well
      OffsetAbs earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      442
    2. 2
      +Edouard
      200
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      FloatingFatMan
      71
    5. 5
      Steven P.
      67
  • Tell a friend

    Love Neowin? Tell a friend!