Problems using window.opener

I have a simple ajax application

A pop-up window with a form will open.

Both form result pages and ajax hava app are javascript file.

In a popup on a form results page, I am trying to call a method from a shared javascript file to apply to the parent window.

My javascript file contains an updateLayer method which, when caleld from the parent window, works fine. I can't see anything when I try to call it from the popup.

Result page in pop-up window

<script type="text/javascript" src="x.js">window.opener.updateLayer("Layer3", "380118179930"); </script>

      

before any html.

Nothing happens in the parent window. I also tried window.parent.

What is the reason and solution for this?

0


a source to share


6 answers


I am guessing it is related to this question , asked by another user who is also called Josh.

In my answer to this question, I tried to explain that functions from a Javascript file included in the parent window will be attached to the window object, so you use window.opener to access that window object to call them.

It looks like you almost solved it, but the problem is that by including src="x.js"

in the script tag from your form response, you are actually overwriting any code you put inside the script. Also, since x.js is included in the parent window, there is no need for it to pop up anyway.



The code for the form response should look like this:

<script type="text/javascript">
window.opener.updateLayer("Layer3", "380118179930");
</script>

      

I removed the attribute src="x.js"

that would otherwise prevent code from executing between tags <script></script>

.

+3


a source


Your problem might be that you have two JavaScript files with the same content while the namespaces are not being applied.

First, your parent includes the file.js file where your updateLayer () is defined. The parent then opens a child window, which also includes the file.js file. If you do, you have two threads in which each can have their own functions and objects without disturbing the others. I am assuming your function is global. This can cause problems if namespaces are not used. It could also happen that your big ajax library creates an iframe and the like and you won't see anything from it because it happens under the hood.

So try it: top.window.opener.updateLayer ("Layer3", "380118179930");



If that doesn't help, try opening a blank window with no file.js included and calling this function from the opener. If that works, wrap the contents of that .js file in a namespace like myNamespace = {.... large file contents inbetween ....}, make two versions of this (or better, dynamically include the content) and make sure you have two different namespaces. JavaScript often doesn't work the way you think it does.

Also make sure the url of your open window has exactly the same domain. This can lead to security issues to prevent the browser from allowing access from the child window to the parent.

0


a source


Josh

Can you tell if the function is triggered at all, as annakata suggests? For instance. by placing an alert box on the first line of the function?

Otherwise: how is the function defined updateLayer

in x.js

?

If it is defined like this:

function updateLayer(layer, result) {
  // ...
}

      

... then it should work fine.

If it is defined like this:

var updateLayer = function(layer, result) {
  // ...
}

      

then it will not be available as a property of the object window

(and therefore not available as a property window.opener

). In Firefox, at least; I have not tested this in IE or other browsers.

Edit: Why is this question marked as "ajax"? AFAICS, all problems are on the client side of the application; not used by ajax.

0


a source


Try the following:

parent.window.updateLayer();

      

in a separate tag <script>

.

I'm not really sure if it works with src = some.js and the inline script at the same time.

0


a source


Since you have assigned the src attribute to the script element, the x.js results will be parsed as JS and the text content of the element will be ignored.

<script type="text/javascript" src="x.js"></script>
<script type="text/javascript">
    window.opener.updateLayer("Layer3", "380118179930");
</script>

      

-1


a source


Create a new function updateLayer in the parent html file. Rename it to something else and call the original updateLayer from there. eg.

function updateLayerPage(arg1, arg2)
{
    updateLayer(arg1, arg2);
}

      

and then call this new function from the child page

window.opener.updateLayerPage("Layer3", "380118179930");

      

-3


a source







All Articles