Reputation: 41
I am working on a project where I am using vanilla javascript and jquery to open a new window when I double click a certain element in a page. I know how to open a new window in javascript/jquery, what I do not know how to do is how to send information from the original window to the new window.
Say I double click something in the original window. This will then create a new object and open a new window. I would like to know if there was a way to send the newly created object over to the new window from the original window.
Upvotes: 0
Views: 717
Reputation: 3231
What you can do is attach an object to the 'window' object of your parent window right before you open the popup. The popup window has access to the parent window, so as long as you know the name of the object, you can inspect it from the popup.
So on your parent page:
<script type="text/javascript">
function DoPopThing() {
var obj = { foo: 5, bar: 12, eep: function () { alert("fools!"); } }
window.prePopObj = obj;
window.open("PopupTarget.aspx", "PopupWindow", "width=400, height=400, resizable=false");
}
</script>
<button onclick="DoPopThing(); return false;">Click me!</button>
And on your popup/child page ("PopupTarget.aspx" in this example):
<script type="text/javascript">
$(document).ready(function () {
alert(window.opener.prePopObj.foo);
});
</script>
The popup window will have access to that object no matter how many times it is refreshed or the page URL changes. As long as the parent window behind it does not refresh (which would cause it to loose its "prePopObj" object), the object is available. The child window can alter the parent window's object as needed as well.
Upvotes: 1