PDA

View Full Version : Loading page onselect



shnawer
08-20-2008, 06:42 PM
Hi
I am trying to get a url onselect option but is not working can anyone look at my script and please correct it???


############################

var xmlHttp

function GetUrl(striurl)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url=strurl;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("mydiv").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

<form id="mf">
<select id="smf" onselect="GetUrl(this.value)"><option value="http://google.com">Get Google</option></select>


<div id="mydiv"><div>

######################


Thnx Vry Mch

Nile
08-20-2008, 06:45 PM
Try this:


<select id="smf" onchange="GetUrl(this.value)"><option></option><option value="http://google.com">Get Google</option></select>

shnawer
08-22-2008, 05:14 PM
No bro its not working!

boogyman
08-22-2008, 05:23 PM
you need to pass the reference of the xmlHttpObject or it will not be present because of the scope.



function GetUrl(striurl)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url=strurl;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged(xmlHttp);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function stateChanged(&state)
{
if (state.readyState==4)
{
document.getElementById("mydiv").innerHTML=state.responseText;
}
}


or you could do the state change processing without changing functions


function GetUrl(striurl)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url=strurl;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange= function () {
if(xmlHttp.readyState ==4)
{
document.write(xmlHttp.responseText);
}
}

xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}