1

I hava a javascript function which opens a new jsp window using window.open(...); Also I have the function passValues(value);

In my child window I do some stuff and eventually i will pass values to my parent javascript like so:

window.opener.passValues(value);

When I try to use 'this' in the function, it refers to window.opener.

This I don't want to happen.. I want that my this remains the this that it was before I called the passValues function.

Thank you for your help

3 Answers 3

1

This happens because in event handlers "this" refers to the function called. So is necesary to make a reference: var self = this inside the callback. And use "self" as "this"

Sign up to request clarification or add additional context in comments.

Comments

0

Invoke the function with Function.prototype.call or Function.prototype.apply.

window.opener.passValues.call( this, value );

That would invoke .passValues with the current value of this, to nail it down to the current window object, just call

window.opener.passValues.call( window, value );

.call() and .apply() both offer the option to set the value for the *this context variable` within the function of invocation.


Reference: .call(), .apply()

2 Comments

this still isn't the solution, because I call the function when i click a button.. When I'm in the passValues function the this is now the childwindow and I want it to be the javascript file
@user1345883: I'm afraid I don't understand that. this always refers to an object / *context. So What exactly do you mean with "the javascript file" ? You can pass any object-reference to .call() and .apply(), the this value will than refer this object within the invocation.
0

In your child window, call the parent function with call or apply and pass the scope of your child window with this

window.opener.passValues.call(this, value);

or if you want to pass more than one argument, use apply

window.opener.passValues.apply(this, [value1, value2]);

However, if you call the parent function in the context of the child scope (this), you're able to access global variables of the child window with this.value, so passing the values might be obsolete.

Be aware that the value of this depends on the context of the function call itself... For example, if you call it from within an onclick handler of a link (BTW don't do that), this referes to the context of the link, not to the child window. Same when you call it from within a instantiated object (with new), this referes to the scope of the class and not to the child window. In doubt, replace this with that object, who's scope you want the function be called in.

 window.opener.passValues.call(window, value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.