Window.opener with child window navigate in same window not working - javascript

I have scenario like P is parent page and from that page one child page is being open using window.open(C) where C is child page. Now I have to navigate in same child window. Let's call that page as C1.
I found when I try to access one button of page P from page C1, it gave me not found.
My syntaxt is like (in page C1),
window.opener.document.getElementById('xyz');
Is there any way using which I can identify the page I am getting in window.opener is same page what I am looking for?

You ask:
Is there any way using which I can identify the page I am getting in window.opener is same page what I am looking for?
Just an idea: generate some random value before opening the new window and store it in the parent window, write this value to the new window and cross-reference.
Update:
You can share data between parent and child browser windows at some scenarios as shown below:
window.opener.functioname(arg1,arg2,..)
- to send data to the parent window from child window
winobj.functioname(arg1,arg2,..)
- to send data to the child window from parent window using the window handle got from window.open()
See for a lot more info on this page.
Good luck!

I am using FCKEditor on my parent page and child page opened from that editor only. So I have to have use window.opener.parent.my_function();
Thanks a lot to all who helped me.

Related

Issue accessing parent URL from child window

Suppose parent window has abc.xyz.com url and, I am opening a child window using: window.top.open
I am getting parent window object in child window using window.opener and polling the parent window url using window.opener.location.href.
Now, if the user click on a link in the parent window, which navigates to def.xyz.com then the window.opener.location.href is giving 'Permission Denied' and window.opener.closed is returning true. (in child script)
I need to change my child window if the parent window is not inside xyz.com
How do I know that the parent is navigated to xyz.com or some other domain?
You can add some javascript to the parent window that modifies the child when it's URL changes. Using the window.onunload event would do the job:
window.onunload=function(){
childWindow.location.href = 'parentURLChanged.html';
};
Instead of changing the location completely you could also only add a hash to the URL so It would change from child.html to child.html#parentHasChanged.
If you want more complete communication between these two windows I suggest you use the jQuery postMessage Plugin. It allows you to send and receive messages between two Windows or Frames and also works cross-domain.
Something like this:
query = window.parent.location.search.substring(1)
referenced from HERE. This one can also help.

popout browser windows still part of main page

can browser popout windows be launched (like google talk conversation windows can be popped out into a new window from the main gmail page.) so they are still part of the main page i.e. share resources and access and modify each other, e.g. if the popout window contained a button it can be clicked and modify part of the original page without having to go to the server? I'm guessing the answer is no, but as I don't know for sure I thought I'd ask.
You can access the popout parent by using the window.opener property from within the 'popout' window. That will give you the window object of the parent.
So if on your main page you have a global variable test
var test = 'Hello';
It can be accessed from the child window using
window.opener.test

Can I open a new window and change the DOM of the calling window in JavaScript?

First I would like to say that I've been programming JavaScript for about 3 months now and also that I'm not very concerned with solving this problem by the standards or best practices. My main concern is learning to use the DOM. I don't want to use any jQuery because I'm not familiar with it.
I'm trying to make a non-profesional "login" function on my page, using JavaScript and the DOM. To begin with I was using "login" screen that would be displayed "hidden" initially and then be displayed "block" when in use. This worked fine and looked really good when I added a darkened screen behind the "login" screen by adding less opacity (0.1 opacity) to the main part of the page that's beneath the "login screen".
This (the opacity) would return to normal when I closed the "login" screen. So you can see all the stuff is happening within the same page using the same DOM. OK, this is how I wanted it to work: you create a username then you your create a password. Boom, finished!
But here's the problem: after you create a username and password I want it to say "Hello (username here)" where the login link initially was. I could just use the DOM and insert the username into the HTML page, but when I submit the form that is the login function the page gets reloaded and the changes to the DOM become erased!
So now I can tell you about the solution I thought of: make the form (login page) be in a new window, so when the form is submitted (and the DOM is manipulated) the new window is reloaded and then subsequently closes leaving the changes to the main pages DOM intact. Only problem is I can't figure out how to do this. You could probably say that's a major problem hahaha.
So, can I manipulate the parent windows (i.e. the calling window) DOM from the new window?
In response to your answer: you can modify the caller window's DOM by using window.opener.document from the new window;
window.opener is a reference to the caller window (if any, otherwise null), but only if both windows are from the same domain).
NOTE: Is it a small webpage or you are going to do a lot of DOM manipulation on a web site/application via javascript? In the later case you should use a javascript library/framework (I recommend jQuery) in order to do the dirty job more easily.
A popup window can find the window that opened it using the opener variable.
If both the popup window and the original window originate from the same domain, then the popup window can indeed modify the HTML of the original window.
If the popup window and the original window contain content from a different domain, then they can't see the HTML of each other - due to the cross-origin protection that browsers put in place.

Refresh the parent window from the second child window in Javascript

The work flow is like this, from my page, I created a popup using window.open method, then open other popup from that window and close its immediate parent, and I need to refresh the first parent page from this popup. I tried in different ways, but the window.opener method can't get the parent windows property.
Can anyone help me to solve this?

setting opener property on iframe

The framework I'm using (Django admin) pops up a new window when adding new items to the database. I would like to use a light box instead of popping up a new window (fancybox for now), and I'm using the iframe option. However, when I click save in the iframe, a request gets sent to the server, and the server responds with a page contains only the JavaScript code: opener.dismissAddAnotherPopup(...). This function basically closes the window and refresh the fields in the parent (opener) window. Since I don't have opener set, it returns an error and never closes the iframe. Is there anyway of setting opener on the iframe so that it won't be null?
Thanks a lot!
Jason
The way to do this is to override the admin view that sends the javascript. These are contained in django.contrib.admin.options: the ModelAdmin.response_add and ModelAdmin.response_change methods. You can simply override these in your model's admin class so that they return the correct fancybox closing code.

Categories

Resources