Hi to all,
I am new to javascript. Can anyone help me regarding this browser Compatibility issue. Its working well in IE 9. But its not working in FireFox and chrome
Here is my code
function createRequest()
{
var ajaxRequest;
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}
function onSelectedChanged() {
var countryId = $("#ddlCountry option:selected").val()
if (countryId != '') {
var url = url + parseInt(countryId);
var xmlhttp = null;
xmlhttp = createRequest();
xmlhttp.open('GET', url, true);
xmlhttp.send(countryid = countryId);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var select = $('#ddlState');
document.getElementById('ddlState').innerHTML = "";
var data = xmlhttp.responseXML;
var M = data.getElementsByTagName("StateId");
select.append("<option value=1>Please the State</option>");
for (i = 0; i < M.length; i++) {
select.append("<option att='" + data.getElementsByTagName("StateId")[i].childNodes[0].nodeValue + "'>" + data.getElementsByTagName("StateName")[i].childNodes[0].nodeValue + "</option>");
}
} else {
You can easily achieve this using jquery.ajax function.
function onSelectedChanged() {
var countryId = $("#ddlCountry option:selected").val()
if (countryId != '') {
var url = url + parseInt(countryId);
$.ajax({
url:url,
type:'GET',
dataType:'xml',
data:{countryid:countryId},
success:function(data){
var select = $('#ddlState');
select.empty();
//data consist of your xml data
//now do you xml parsing here.
},
error:function(xhr){
alert(xhr.responseText);
},
complete:function(){
}
};
}
else {
$('#ddlState').empty();
}
jquery is much more simpler and on top of it you don't have to worry about multi browser issues.
@Rangineni - I agree with the others, you should really consider using JQuery. It's a corss browser javascript library. One of the reasons the JQuery library was written was to handle cross browser issues for you. For example, JQuery has an ajax function
that works with all major browsers, you no longers have to write a lot of code and check for the different ajax objects.
CyberBud has already translated your code, but I wanted to also show you how simple it is to write an ajax call in JQuery. Assume you want to pull data from a url. It's as simple as this
$.get(url, function(response){
//response has the data returned from the server
});
That's obviously a simple example that users an ajax shortcut called get, but JQuery gives you functions to handle a success, a failure and much more.
Rangineni
0 Points
19 Posts
Browser Compatibility Issue
Apr 27, 2012 06:04 AM|LINK
Hi to all,
I am new to javascript. Can anyone help me regarding this browser Compatibility issue. Its working well in IE 9. But its not working in FireFox and chrome
Here is my code
function createRequest()
{
var ajaxRequest;
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}
function onSelectedChanged() {
var countryId = $("#ddlCountry option:selected").val()
if (countryId != '') {
var url = url + parseInt(countryId);
var xmlhttp = null;
xmlhttp = createRequest();
xmlhttp.open('GET', url, true);
xmlhttp.send(countryid = countryId);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var select = $('#ddlState');
document.getElementById('ddlState').innerHTML = "";
var data = xmlhttp.responseXML;
var M = data.getElementsByTagName("StateId");
select.append("<option value=1>Please the State</option>");
for (i = 0; i < M.length; i++) {
select.append("<option att='" + data.getElementsByTagName("StateId")[i].childNodes[0].nodeValue + "'>" + data.getElementsByTagName("StateName")[i].childNodes[0].nodeValue + "</option>");
}
} else {
}
};
}
else {
document.getElementById('ddlState').innerHTML = "";
}
}
Please can anyone help me pls. Its very very urgent.
Thanks
Kumar
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: Browser Compatibility Issue
Apr 27, 2012 06:25 AM|LINK
what is the error line?
Rangineni
0 Points
19 Posts
Re: Browser Compatibility Issue
Apr 27, 2012 06:35 AM|LINK
Hi Ignatandrei
No error. But I am not getting the response and data. Here var data = xmlhttp.responseXML; I am waiting for our reply. Its very urgent.
Regards
Kumar
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: Browser Compatibility Issue
Apr 27, 2012 07:46 AM|LINK
put alert after each line. What alert you do obtain latest ?
More, I do not know so well xmlHttp - maybe you can transform via kquery ajax - see http://bit.ly/mvc_ajax_jquery
Rangineni
0 Points
19 Posts
Re: Browser Compatibility Issue
Apr 27, 2012 07:54 AM|LINK
Hi Once i pass the url as request. Then its getting null as reponse.
Regards
Kumar
ossprologix
Member
394 Points
128 Posts
Re: Browser Compatibility Issue
Apr 27, 2012 11:30 AM|LINK
use jQuery it's compatible and u wudn't have to write boiler-plate code
cyberbud
Contributor
3298 Points
551 Posts
Re: Browser Compatibility Issue
Apr 27, 2012 11:55 AM|LINK
You can easily achieve this using jquery.ajax function.
function onSelectedChanged() { var countryId = $("#ddlCountry option:selected").val() if (countryId != '') { var url = url + parseInt(countryId); $.ajax({ url:url, type:'GET', dataType:'xml', data:{countryid:countryId}, success:function(data){ var select = $('#ddlState'); select.empty(); //data consist of your xml data //now do you xml parsing here. }, error:function(xhr){ alert(xhr.responseText); }, complete:function(){ } }; } else { $('#ddlState').empty(); }jquery is much more simpler and on top of it you don't have to worry about multi browser issues.
MCP(.net 3.5)
Nepal
ishwor.cyberbudsonline.com
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Browser Compatibility Issue
Apr 27, 2012 04:38 PM|LINK
@Rangineni - I agree with the others, you should really consider using JQuery. It's a corss browser javascript library. One of the reasons the JQuery library was written was to handle cross browser issues for you. For example, JQuery has an ajax function that works with all major browsers, you no longers have to write a lot of code and check for the different ajax objects.
CyberBud has already translated your code, but I wanted to also show you how simple it is to write an ajax call in JQuery. Assume you want to pull data from a url. It's as simple as this
$.get(url, function(response){ //response has the data returned from the server });That's obviously a simple example that users an ajax shortcut called get, but JQuery gives you functions to handle a success, a failure and much more.
You can learn more about JQuery Here
http://jquery.com/
Blog | Twitter : @Hattan
Rangineni
0 Points
19 Posts
Re: Browser Compatibility Issue
May 02, 2012 06:18 AM|LINK
Hi cyberbud
Even i use this above it throws an error as 'undefined' Here is my code:
function onSelectedChanged() {
var countryId = $("#ddlCountry option:selected").val()
if (countryId != '') {
var url = 'http://192.168.0.60:4466/RestDataService/state?countryid=' + parseInt(countryId);
$.ajax({
url: url,
type: 'GET',
dataType: 'xml',
data: { countryid: countryId },
success: function (data) {
var select = $('#ddlState');
select.empty();
var count = data.getElementsByTagName("StateId");
select.append("<option value=1>Please the State</option>");
for (i = 0; i < count.length; i++) {
select.append("<option value='" + data.getElementsByTagName("StateId")[i].childNodes[0].nodeValue + "'>" + data.getElementsByTagName("StateName")[i].childNodes[0].nodeValue + "</option>");
}
},
error: function (xhr) {
alert(xhr.responseText);
},
complete: function () {
}
});
}
else {
$('#ddlState').empty();
}
}
Thanks
Kumar
Young Yang -...
All-Star
21740 Points
1825 Posts
Microsoft
Re: Browser Compatibility Issue
May 04, 2012 02:58 AM|LINK
Hi
As your question is related to javascript, please post your question to here:
http://forums.asp.net/130.aspx/1?HTML+CSS+and+JavaScript
You will get more professional help from there.
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store