Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call JavaScript in one Window from another Window 1

Status
Not open for further replies.

NFI

Programmer
Jun 7, 2000
278
GB
...that's about it, actually :)

I use window.open to open a new window and I want to be able to call a JavaScript function in that new window from the opening window. I've tried this;

newWindow=window.open(newWindow.html)
newWindow.JSFunction();

...but I got an error about the object not supporting that property or method - I can't say I was shocked to the very core of my being :(

Is is possible?

Any help would be much appreciated,

Thanks,

Paul
 
Hi

It works for me like the below. Show us your code which does not work.
Code:
<html>
<body>
<form name="f">
<input type="button" value="Open new window" onclick="w=window.open('b.htm')"><br>
<input type="text" name="t">
<input type="button" value="Send to new window" onclick="w.runme(document.f.t.value)">
</form>
</body>
</html>
Code:
<html>
<head>
<script type="text/javascript">
function runme(what)
{
  document.getElementById('h').innerHTML=what
}
</script>
</head>
<body>
<h1 id="h">B</h1>
</body>
</html>
If those two commands are executed immediately one after the other, the browser may not load the page in the give short time.
Code:
newWindow=window.open(newWindow.html)
setTimeout('newWindow.JSFunction()',1000)

Feherke.
 
It's probably because this is totally invalid syntax:

Code:
newWindow=window.open(newWindow.html)

You've no quotes around the URL, so I'm surprised it even opens the window.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hello,

thanks for your suggestions, but I'm still having no luck.

I realise I missed out the quotes in my code above, but it was only sort of pseudo-code to explin what I was trying to do...sorry about that.

Here's my actual code:

This is in the window I'm opening from my first window:
Code:
  function load_mainImage() {
    mainImage.src=arrGallery[galleryImage.value][0];
    if (arrGallery[galleryImage.value][3]=="H")
    {
      holder.style.left="40";
      mainImage.style.width="760";
      mainImage.style.height="570";
    } else {
      holder.style.left="200";
      mainImage.style.width="425";
      mainImage.style.height="570";
    }
    imgDate.innerHTML = arrGallery[galleryImage.value][1];
  }

arrGallery is a multi-dimensional array which contains the path to the images, their orientation, notes about them, their date etc
galleryImage is an <input type=hidden... which holds the index to the array.
holder is a <div> which contains <img id='mainImage'...

This is the code that opens that window:
Code:
      function showFullImage(strImage,strWidth,strHeight,intImage)
      {
        viewer=window.open("galleryViewer.htm")
        viewer.document.getElementById('galleryImage').value=intImage;
        viewer.load_mainImage();
      }

So, in theory, this finction should open the gallery viewer window, update <input type='hidden' id='galleryImage'... with the index of the picture selected and then call the load_mainImage() function, but it doesn't. Instead, <input type='hidden' id='galleryImage'... is left as its default value and I get an error in the calling window of "object doesn't support this property or method" and it's the function call that's throwing it...

Thanks for your help again, but any more ideas?

Thanks,

Paul

 
It could be that the popup document has not loaded before you are trying to access its elements.

I suggest trying just this:

Code:
var windowHandle = null;
function showFullImage(strImage, strWidth, strHeight) {
   windowHandle = window.open('galleryViewer.htm');
}

fuction viewerLoadedCallBack() {
   windowHandle .document.getElementById('galleryImage').value = someIntValueHere;
   windowHandle .load_mainImage();
}

and then in the popup have an onload event which calls "window.opener.viewerLoadedCallBack();"

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
... Obviously without the erroneous spaces my browser seems to have pasted after the "windowHandle" variable in the latter function.

You could also remove the params passed to "showFullImage", as you're not using them at all.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Blimy, that's clever!

I had to modify it a bit to keep hold of the integer value, but that works a treat!

Please accept this star by way of thanks :)


Paul
 
Thanks :)

You could possibly have also set the callback up from the opener... to save having to keep hold of the int... although I've not tested this so it might not work if you call the opener twice in quick succession:

Code:
windowHandle = window.open('galleryViewer.htm');
windowHandle.onload = function() {
   windowHandle.document.getElementById('galleryImage').value = theIntYouDidntNeedToPassAroundAnyMore;
   windowHandle.load_mainImage();
}

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top