views:

80

answers:

1

I have a web application that consists of two websites - one running on port 8080 (java) and other running on port 80 (IIS). The java web pages call into IIS web pages, which occassionally call back into java web pages to get additional information. The javascript that handles the communication/ exchange of data works in IE but not in Firefox.

Page 1 (IIS) > onClick of Button > Page 2 (Java/Tomcat) > User closes popup > Data transfer to Page 1

Here is the Javascript

if(window.attachEvent){//IE exclusive method for binding an event
  alert("AttachEvent");
  window.attachEvent("onfocus", Focus_onfocusEvent); 
  window.objExitPopupWindow.attachEvent("onunload", Focus_onunloadExitEvent);
 }else if(window.addEventListener){//DOM method for binding an event. W3C standard   

  try{
   alert("Event Listener");
   window.addEventListener("focus", Focus_onfocusEvent, true);
   window.objExitPopupWindow.addEventListener("unload", Focus_onunloadExitEvent, true);
  }catch(err){
   alert(err);
  } 
 }

The issue I run into is a) In firefox, if I have the alert("EVent Listener"); enabled then I get an error about Error: Permission denied for "http://localhost" to get property Window.addEventListener from "http://localhost:8080". b) In firefox, if I do not have the alert on, then there is no error message shown but it looks like it does Focus_onuloadExitEvent first then Focus_onfocusEvent, all in the process of opening the popup. Closing the popup does not fire Focus_onunloadExitEvent.

How do I make sure that the code behaves properly in both Firefox and IE - ie. I want the onfocusEvent to fire on popup focus and onunloadEvent to fire on popup unload.

TIA

+1  A: 

This is the same origin policy. You cannot have JavaScript loaded by a document on host A access or modify content in a document loaded from host B. Since one of your hosts is localhost:80 and the other is localhost:8080, these are considered to be different hosts by the browser.

In your particular case, a solution would be to put the popup page on the same server as the page that opens it and put an iframe inside the popup pointing to the content on the other host. That way your JavaScript will be able attach event listeners to the popup itself.

Mike