Firefox AddEventListener issue
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 occasionally 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
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 messa开发者_开发问答ge 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.
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.
精彩评论