Forum Moderators: open
I have this snippet of code, wich is opened in a pop-up window and wich should update the drop-down menu of the opener window. I was doing this fine, when it was opened in an iFrame in the same window, but now that I decided not to use this iFrame anymore, Iīm doomed!
function UpdateMenu(){
var field = window.opener.document.form1.city;
field.options.length=0 ; // erases prior options. This works!
var group = new Array( "Select your city", "Kansas","New York","New Jersey","Boston","Houston" );
for (i=0;i<group.length;i++){
field.options[i]=new Option(group[i],group[i],false,false); // the javascript debugger points the error here! Why!?
}
}
The browser prompts a javascript error message saying the server has commited an exception (?!) and stops. Nothing is updated anymore! And the browser even wants to send f*ing Bill Gates a message reporting it!
Please help me, you are my last hope!
Thanks a lot
phoenix_fly
PS: By the way, I did take a look at xmlHttpRequest to do that, but it doesnīt seem to allow me to execute back a javascript like this. At least I canīt imagine how to parse the response text and throw it back for execution... If you guys do, I am willing to abandon the pop-up solution...
I donīt know if you guys didnīt answer because it was way too difficult or because my question was way too stupid ...lghs
But Iīve just managed to make it work, with our friend xmlHttpRequest! As I told you, I was willing to abandon this dirty solution of the pop-up, and the only thing I had to do is to understand the reply dialog from the server to the xmlHttpRequest listener handler. And itīs simple.
Iīll post it up here, for the sake of the comunity:
function MenuCity () {
var state = document.form1.state.value;
if ( state!= "" ) {
var location = 'script.cgi?state=' + state;
http.open("GET", location, true);
http.onreadystatechange = handleHttpResponseCity;
http.send(null);
document.form1.city.options.length=0;
document.form1.city.options[0]=new Option('please hang on...generating menu','0',false, false);
}
}
function handleHttpResponseCity() {
if (http.readyState == 4) {
var cities = new Array();
cities = http.responseText.split(","); // the script.cgi will just print the http text/html header and then the cities, sepparated by commas. Yeah, itīs as simple as that, you do not have to worry about any html tags, just print it out to the world!
document.form1.city.options.length=0; // reseting the menu
for (i=0;i<cities.length;i++){
document.form1.city.options[i]=new Option(cities[i],cities[i],false, false); }
}
}
And thatīs it! All this code you put in the page that you do not want to refresh. But donīt forget to start the javascript with this code, before the functions above. It is a cross-browser code that you do not need to understand, just use it and it will work.
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!= 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
vxmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject();
Any doubts, just reply!
pnoenix_fly