Reputation: 104
there are few similar questions to this, but none of the solutions worked in my case. I am pretty new at this, so the problem could also be a basic thing that i'm missing.
On the event of click, I am trying to open a new tab with the clicked object, so for now i'm just trying to alert it in the new tab.
the parent window code:
$(document).on("click", ".button", function(){
var toSend = $(this);
var newTab = window.open("test.html", "_blank");
newTab.myData = toSend; // this one was a solution for a similar problem,
// didn't work for me.
});
this is the opened window:
$(document).ready(function() {
console.log(window.parent.toSend);
console.log(window.parent.myData);
console.log(myData);
console.log(window.opener.toSend);
});
first 2 logs are returning "undefined", and the third is throwing an exception that myData is not defined (Uncaught ReferenceError: myData is not defined). for the latter log i get "Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match."
Help will be much appreciated, thank you!
edit: console.log(toSend) does log the proper object, and is defined.
Upvotes: 0
Views: 5021
Reputation: 1074485
In your code, toSend
is a local variable inside a function. To be accessible on the window
object, it has to be a global variable. One way to create a global variable from within a function is to assign to a property on window
. So:
$(document).on("click", ".button", function(){
window.toSend = $(this);
var newTab = window.open("test.html", "_blank");
});
and then
$(document).ready(function() {
console.log(window.opener.toSend);
});
That said, global variables are best avoided, so if you have to do this with multiple pieces of information, use a single global variable which refers to an object (using a name that's unlikely to conflict with things), and put the individual pieces of information on that object as properties.
Upvotes: 1