1

I understand how to open windows and communicate between the original window that opened the 2nd window and the 2nd window to communicate back to the original window. However, I have a situation where I have original page and page 1. These pages are 2 different PHP scripts that runs as a separate page.

page 1 opens a window with clients webpage inside. We will call this window 1.

Original page opens window 2 and window 3. These windows loads in php files that hold editing tools. I need these window 2 and window 3 to communicate with original page to edit window 1's content. I don't see how this is possible. I only seen tutorials and books talking about original page and the windows it creates are the only ones that can access each other. So, I know in theory I cannot have the original page communicate with window 1 because window 1 was created by page 1.

Is, there any way to work around this? The page 1 opens up content...like images, webpages...etc. I have it already setup this way. I need to be able to have my window tools to access the content window. That way they can edit webpages or images. How can I Do this if it's possible?

No, that isn't the same question. I need to modify the document to make changes to the html. So, using cookies won't solve my problem. I need to edit t he content in the other window that was open by a different webpage but it's still part of the same domain.

  • 1
    If those pages are all on the same domain, and *`I understand how to open windows and communicate between the original window`* than... – Roko C. Buljan May 12 '15 at 20:13
  • 2
    are all the pages on the same site? if so, `storage` events are a really handy way to avoid keeping window handles. – dandavis May 12 '15 at 20:13
  • C=page creates A, B. You want to communicate between A and B? – Roko C. Buljan May 12 '15 at 20:15
  • Yes, it's using the same domain. it's just that 2 different webpages of the website will open a window. example: page 1 opens window 1, page 2 opens window 2. How can page 1 access window 2 if it didn't open up that window? – user3897632 May 12 '15 at 22:04

1 Answers1

2

Updated 05/21

For reference: https://github.com/dboots/crossWindowAjax

Demo: http://www.donboots.com/so/index.php and http://www.donboots.com/so/index2.php

This is a fairly rudimentary polling solution between windows that use the /js/editor.js file found in the github repo/

/index.php, /index2.php - These simply open up our popup windows.

/colors.php, /tools.php - Our popup windows that contain the poll() function and also have examples of using the set() function. Both of these are custom functions that live within /js/editor.js

/js/editor.js - This is the bridge between our popup windows and the PHP file (/js/ajax/process.php).

/_classes/Editor.php - This is the class that /js/ajax/process.php uses to set/get our variables. In this example, it sets/gets $_SESSION variables, but this class could be expanded to use any sort of file storage, database, etc.

Original answer (not relevant)

I imagine this could be solved via tossing around window variable scopes.

Does this scenario match your needs?

Window 1

//-- window 1 variable
var page_variable = "page_variable";

//-- handle for window A
var windowA = window.open('windowA.php', 'windowA', 'height=200,width=600');

//-- handle for windowB
var windowB = window.open('windowB.php', 'windowB', 'height=400,width=500');

windowA.php

//-- local windowA variable
var windowA_variable = "windowA Variable";

//-- windowB variable accessed via opener (window 1)
var windowB_variable = opener.windowB.windowB_variable;

//-- window 1 variable
var page_variable = opener.page_variable;

windowB.php

//-- local windowB variable
var windowB_variable = "windowB Variable";

//-- windowA variable accessed via opener (window 1)
var windowA_variable = opener.windowA.windowA_variable;

//-- window 1 variable
var page_variable = opener.page_variable;
Don Boots
  • 2,028
  • 2
  • 18
  • 26
  • No, in your example add in window 1 and window 2 then open window a and B. if window 1 opens window a and window 2 opens window b. Is it possible for window 1 to access window b even though it didn't open window b in the first place? the code you show is opener. which refers o h original window that opened the window. I am talking about having 2 pages. one page opens 1 window and another page opens another window.. how can those pages communicate with the window they didn't open? – user3897632 May 12 '15 at 22:14
  • example: webpage 1 opens window A and webpage 2 opens window b. How can webpage 1 access window b if it didn't open that window? Window A is opened only by webpage 1 . Window b was opened by webpage 2. How can webpage 2 access window A and webpage 1 access window b? – user3897632 May 12 '15 at 22:18
  • From what I researched, you would need to utilize ajax in conjunction with some PHP code to get/set attributes and also utilize long polling. Not sure if you need to strictly use javascript to manipulate windows, but this would be my solution. I can provide assistance with that, if that sounds applicable. Otherwise, some alternatives would **possibly** be Cross-window messaging with PostMessage (http://bit.ly/1F7OCq0) or maybe HTML5 WebSockets (http://bit.ly/18Uy8WA). – Don Boots May 13 '15 at 18:31
  • That's not possible. I am making an webpage layout editor. you can open multiple webpages at a time. I have 1 php file that handles webpages, images, multi-media to prevent from hot linking content. so, that files loads in a list of webpages, images, videos uploaded or created. It loads in he javascript that when you click on a image or webpage. It opens it up in a new window. This can be used to view content or to edit it. In order to edit it. Yo need to click a tools button. This will open up another window... but that button is on the main page. So, there's 2 different pages that ... – user3897632 May 13 '15 at 18:51
  • 2 different pages that end up opening 2 different windows. one opens up the content in a window. The other opens up the tools window. I need to be able to use that tools windows in order to edit the content window selected. In theory I don't see that it's possible for me to access each window directly since each one are opened by 2 different webpages... 2 different php scripts that have JavaScript echoed out. – user3897632 May 13 '15 at 18:54
  • The concept is communicating the actions of your windows/pages using PHP via AJAX. A simple, inefficient example can be seen at http://bit.ly/1KKIYeJ and http://bit.ly/1F8cJEU. One opens up a "tools" window and one opens up a "colors" window for the sake of a relevant example. Even though they were opened separately, they still are aware of each other's actions. – Don Boots May 13 '15 at 21:11
  • @ Don Boots : so, I would have to use cookies or ajax to do this? I cannot do this directly? Can I set cookies via javascript? then with javascript grab the cookies? What would be the best method? If I do ajax how would I still access the other video? – user3897632 May 15 '15 at 00:02
  • Cookies and JavaScript would work, you just wouldn't have as much control over them. I can upload this example to github if you think it will help. – Don Boots May 15 '15 at 16:36
  • what is the poll function? I don't understand how the color window is making color changes to the other window. I don't see ajax code and never used poll. Could you explain this to me? – user3897632 May 21 '15 at 17:55
  • Updated original answer explaining solution. – Don Boots May 21 '15 at 18:20
  • I think I understood but in my project. I have tools that when you click a button it opens up 2 windows. I understood your example and how to do the 3 windows. However, the content that needs to be edited is a 3rd window. However, I load in webpages icons in a div. I load this when the person clicks an icon to see files uploaded. It then loads in webpages icons and folder icons. once clicked it opens the 3rd window. how can I pass variables from that? If it gets loaded after the main page is loaded? – user3897632 Aug 12 '15 at 19:56
  • I mean like in the div if I open a window from code inside the div. Would that opener still reference the main page or main window? Here's what we have main window, window 1, window 2, window 3. main window is the actual main page. On this page it has a div that is empty but once clicked icon uploaded files. It uses ajax to load in all the files in icon form in the div. When you click the icon it opens window 3 aka the content window. The main window has a button outside of that div that when clicked opens window 1 and window 2 which are tools. I am just wondering if I need to do – user3897632 Aug 12 '15 at 20:10
  • anything in the div area. Because that icon and the javascript code for it resides inside the div. I am not sure when opening the window if it will reference the main page. I assume it should do that since it's a div and not a iframe. So, I can post variables in the main window which is the opener for all windows...not sure about the one in the div. I can use the main window to post variables that way all 3 windows can interact with each other. – user3897632 Aug 12 '15 at 20:15
  • as long as you track the root opener (i.e. the main window) across your windows, you can set/get the root values of your application. – Don Boots Aug 13 '15 at 15:13
  • What do you mean by tracking it? Main window is the actual webpage where it opens window 1,2,3. It's just that when you open window 1.. you open it from code inside the div that is loaded after the main webpage was loaded. I was thinking to put my variables on the main webpage. Then have windows 1,2,3 manipulate them via the opener reference. I just am not sure if code inside the div will use opener to refer to the main web page or will it reference the place inside the div. I am thinking I am confusing divs with iframes. I just want to know if it matters if the code is inside a div or not. – user3897632 Aug 13 '15 at 23:47
  • or not when using the opener to refer to the main webpage. If the code is inside a div and it opens window 1. Would window one still when referring to opener still point to the main webpage window? I just want to know if there will be a conflict? – user3897632 Aug 13 '15 at 23:49
  • I'm referring to tracking it via code. Always making sure that regardless of what window you're in, you have a reference to the main window. – Don Boots Aug 14 '15 at 13:28