responseText contains null/undefined in safari - javascript

Here is my js and problem:
function refreshDiagValues()
{
var xmlhttp = null;
var recv;
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");
}
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
recv=xmlhttp.responseText;
document.getElementById("text7").innerHTML = recv;
}
};
xmlhttp.open("GET","ajax_DGS0101", false);
xmlhttp.send();
}
I could get this work in IE and chrome, but fails in safari.
I tested it by changing document.getElementById("text7").innerHTML = 5 and it shows the correct number on all browsers.
It feels like responseText does not contain any value for safari, but contains results for chrome and IE.
Could anyone help me?

Related

XMLHttpRequest() is not working in Opera mini

I just Trying to make an asynchronous project with XMLHttpRequest. it works well on other browsers but not working on Opera Mini Mobile Browser. now tell me how can I fix this.
Code:
function runxml(){
var xmlhttpr;
var setup = document.getElementById("setup");
if(window.XMLHttpRequest) {
xmlhttpr = new XMLHttpRequest();
} else {
// for IE8 and Earlier
xmlhttpr = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttpr.onreadystatechange = function(){
if(this.readyState == 4 || this.status == 200) {
setup.innerHTML = this.responseText;
}
}
xmlhttpr.open("GET", "file.txt", true);
xmlhttpr.send();
}

Javascript div refresh

I am trying to refresh a php script to show updated content as a database updates. I first built my php, then the code to refresh and then merge them. However, the script doesn't update. Anyone know why?
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(
function() {
setInterval(function() {
if(document.getElementById('gallery') != null){
function showLots() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("gallery").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php",true);
xmlhttp.send();
}
}
}, 3000);
});
</script>
Thanks.
You did not called method showLots, First define it outside the function and than call it in setInterval
The issue with your code is that function showLots() is inside your if (document.getElementById('gallery') != null) conditional without the function actually executing.
Below is what your corrected code could could look like by moving the function showLots() definition up. showLots() is then called inside where you originally had the definition.
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
function showLots() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("gallery").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php",true);
xmlhttp.send();
}
$(document).ready(function () {
setInterval(function () {
if (document.getElementById('gallery') !== null) {
showLots();
}
}, 3000);
});
</script>

Ajax weird behavior in IE8

Here's the code I use to ajax more reviews:
function showMoreReviews(str) {
var counter = Number($('#counter').val());
var xmlhttp;
if (str == "") {
document.getElementById("reviews").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
counter = counter + 10;
$('#counter').attr({ value: counter });
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//document.getElementById("reviews").innerHTML = xmlhttp.responseText;
$("#reviews").append("<div id='rnum" + counter + "'>" + xmlhttp.responseText + "</div>");
$("#rnum" + counter).hide().fadeIn(800);
}
}
console.log(str);
console.log(counter);
xmlhttp.open("GET", "/MoreReviewsAjax.asp?ml=" + str + "&c=" + counter, true);
xmlhttp.send();
}
It works fine in all browsers except in IE8.. Now here is the weird thing - the code will work if in IE8 I go to dev tools and start debugging for scripts. Otherwise it doesn't work.
PS I am using virtual PC and Windows XP w/ IE8 for IE8 tests.
Your console.log() calls are the problem.
You can add a cheap "polyfill" to your system:
if (!('console' in window)) {
window.console = {
log: function() {},
dir: function() {},
// whatever other console functions you use
};
}
Those dummy functions won't do anything, but they'll keep IE from losing it's mind.

What causes error in IE7?

I am using this JS code to reach a list of cities. It works in fireworks, chrome .e.t.c. But in ie7 it does not. the line document.getElementById(oDiv).innerHTML=xmlhttp.responseText; causes the error.
When I change "responseText" to "readyState", "statusText", "readyState" the scripts works. Only "responseText" causes problem.
What is the problem here?
function showAjax(oDiv, countrycode, dowhat) {
if (oDiv == "") {
document.getElementById(oDiv).innerHTML = "";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open('POST', 'ajax.php?dowhat=' + dowhat + '&countrycode=' + countrycode, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(oDiv).innerHTML = xmlhttp.responseText;
//document.getElementById(oDiv).innerHTML=xmlhttp.readyState;
}
}
xmlhttp.send();
}
Click
<div id=citytd></div>
IE7 (and before, and IE8 in "compatibility" mode) has issues with getElementById, where it considers some things IDs that it shouldn't. I suspect you have a global variable, or another element with name="citytd", and IE is pick that up instead (even though, again, it shouldn't). More: ...by any other name, would smell as sweet

IE6 and 7 issue with innerHTML

IE6 and 7 issue with innerHTML
I have used ajax in the application i have develop, but there are issues with IE6 and IE7, they doesn't support innerHTML. What must be used to fixed this issue and to be a cross browser compatible?
the sample code looks like this.
function showFAQ(src, target){
xhr.onreadystatechange=function(){
if(xhr.readyState == 4 && xhr.status == 200){
document.getElementById('resultDiv').innerHTML=xhr.responseText;
}
}
str = "?action=get&request="+src;
xhr.open("GET", "./requests/data.php"+encodeURI(str), true);
xhr.send();
}
In FireFox, IE8 and other major browsers works fine. Just the problem is with IE6 and 7.
Any help/advice will be appreciated.
Thanks
IE cannot update readonly elements using innerHTML... consider this too.. :)
Try
var xmlHttp;
function getXmlHttpObject() {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer 6+
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
var xhr = getXmlHttpObject();
Update
Try adding
xhr.send(null);
after
str = "?action=get&request="+src;
xhr.open("GET", "./requests/data.php"+encodeURI(str), true);
innerHTML is supported as of IE5. I think you problem is the use of the xmlhttprequest object. That one is only supported as of IE7. You can however ActiveXObject as stealthyninja's code uses.

Categories