This question already has answers here:
what is the json data format ie understands?
(2 answers)
Closed 9 years ago.
I am using ie8 and so does everybody else. I need to make this script work for ie8. I cannot make json.parser work on IE. I tryied some of the recommendations that I saw here like json2.js modify html file etc. It does not seem to be working. Is there an alternative to this method? I really need this working and cannot figure out.
get_cpu.php outputs the data as this:
[1230000000,23]
This is my function. It works on chrome, firefox but not IE.
$(document).ready(function() {
function request_cpu_Data() {
$.ajax({
url: 'get_cpu.php',
success: function(data) {
alert(data.length);
var myObj = JSON.parse(data);
var point = cpu_chart.series[0].points[0];
var newVal=myObj[1];
myDate=myObj[0];
point.update(newVal);
setTimeout(request_cpu_Data, 1000000);
},
cache: false
});
}
It possibly throws an exception because it cannot parse the data that your server returns.
I suspect IE8's implementation cannot parse JSON arrays.
You can try this, for example: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
Related
This question already has answers here:
How to detect online/offline event cross-browser?
(15 answers)
Closed 8 years ago.
Jquery Code which check the internet/network is there or not(mobile/PC/Tablet).It must just check on page load.I thinkAjax will good because Ajax will check after certain interval.
I am looking just like http://tomriley.net/, But it is plugin, I am looking for simple jquery/Javascript.
Its static page which check system internet only.
Any idea is appreciated.
You might try a $.ajax() invoication with a .fail() handler, for example JQuery's getJSON():
var network_available; // bool
var url = '/some/json/call'; // must be relative to the site that
// you are already addressing
$.getJSON(url, function(data) {
network_available = true;
})
.fail(function() {
network_available = false;
});
Though I doubt this will solve all of your problems. The Javascript engine won't allow 'foreign' URL's, just the domain that the script or page was received from. So you'd not be really testing network availability, but also whether your site is up and responding within a reasonable time.
I am currently developing a single page application, using phonegap. What i am trying to realize is to initially parse a xml file and store the content for further usage.
At the moment everything works fine but there is one major problem:
Within my xml file there is a a tag which contains html formatted text content. For example:
<textfield> <h1>Title</h1> Content </textfield>
What i currently do is to load my xml file via jQuery ajax call and then use the html() method to retrieve my textfield html:
this.description = $(Obj).find("textfield").html();
On Google Chrome, Firefox and Android this works fine. The html is stored an can later be appended to my objects. However on Safari and therefore on Iphone devices the html() does not work. Now i am looking for a workaround. I certainly do not want to use text() because my formatting will be ignored.
EDIT: My ajax call:
$.ajax({
type: "GET",
url: Controller.baseURL+"/"+name+".xml",
async: false,
timeout:3000,
dataType: "xml",
success: function (data) {
alert("succ");
xml = data;
},
error: function () {
alert('error!');
}
});
Maybe someone can help me.
Thanks in advance
var parser = new DOMParser();
var parsed = parser.parseFromString(yourXMLStringLoadedViaAjax, 'text/xml');
var textfieldConetent = parsed.querySelector('textfield').innerHTML;
After a few hours of search I finally found my answer here:
https://stackoverflow.com/a/25789924/3566334
It seems that
In IE 9, 10 and 11, Safari 7.0 and Opera 12, the nodes in doc have neither an innerHTML field nor an xml field
I followed the advice of using XML Serializer and it works like a charm!
This question already has an answer here:
how to make Correctly a javascript if exist (folder) command?
(1 answer)
Closed 8 years ago.
Hello stackoverflow users
I have been trying for some time to design a script.
In this script I will look for commands with an if query whether a folder exists.
I do this by using a variable make so shall he find the path using the variable.
So as follows "backgrounds /" + variable;
Here's my script:
var mapname = "dolls";
$.get( "backgrounds/" + mapname )
.done(function() {
var eld = mapname ;
}).fail(function() {
var eld = "default";
})
I'm using JQuery version 1.3.2.
I get the following error in the JS Console
Uncaught TypeError: Object #<XMLHttpRequest> has no method 'done'
does anyone know how I write this code right?
thanks ahead...
Your code will attempt to do a GET-request to the URL you specified, which doesn't necessarily mean that it is a folder. If you try this on your local machine without a web server and provide a folder name, the browser will look for "index.html" or "index.htm" in that folder. If it finds one, it will succeed, which is not really what you're after..
About actual filebrowsing. Javascript does not allow file browsing for security purposes.
JQuery 1.3 ajax GET request would look something like this
var mapname = "dolls";
var eld;
jQuery.ajax({
type: "GET",
url: "backgrounds/" + mapname,
success: function(response) {
eld = mapname;
},
error: function(msg) {
eld = "default";
}
});
Here's the Fiddle
I am trying to read the XML of an RSS feed on a website (that I do not have control over) and display it using Javascript. I used the following code to extract the data and it works, but the major caveat is that this will only work in IE 8 (Ugh).
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",<URL with xml extension>,false);
xmlhttp.send();
setTimeout("wait()",3000);
function wait()
{
alert("complete");
}
var xmlDoc = xmlhttp.responseXML;
var data = xmlDoc.getElementsByTagName("entry");
The wait function currently exists to give the server time to respond to my xmlHTTP request. Does anyone know a workaround in Javascript, AJAX or something similar that would give me functionality on current versions of Firefox, Chrome, etc?
I think you should use JFeed, its faster and its really easy to use, your code is a bit long, with Jfeed you can do it like that:
jQuery.getFeed({
url: '<URL with xml extension>',
success: function(feed) {
alert(feed.entry); //your element
}
});
let me know if you need help with that.
Good luck.
I'm working on a Drupal 6 module where I use jquery (and more specifically, the $.ajax method) to retrieve a RSS feed from Yahoo's weather API. I decided against using the JFeed library because I need access to the elements with "yweather" prefix (and I couldn't find a way of accessing them via JFeed). I decided to use the $.ajax method and parse the XML response instead. The JavaScript code below works fine in Firefox and IE but does not work in Safari (or Chrome FWIW):
function parseXml(xml) {
var atmosphere = xml.getElementsByTagName("yweather:atmosphere");
var humidity = atmosphere[0].getAttribute("humidity");
$('#weatherFeed').html("Humidity: " + humidity);
$('#weatherFeed').append(
"<div style=\"text-align: center;margin-left: auto; margin-right: auto;\">" +
city + ", " + state + "</div>");
}
function getData(){
$.ajax({
type: 'GET',
url: 'proxy.php?url=http://weather.yahooapis.com/forecastrss&p=94041',
dataType: 'xml',
success: function(xml) {
parseXml(xml);
}
});
}
if(Drupal.jsEnabled) {
$(function() {
getData();
setInterval("getData()", 30000);
});
}
When I check the error console in Safari I see the following error message: TypeError: Result of expression 'atmosphere[0]' [undefined] is not an object. Is there an issue with using getElementsByTagName in Safari? Should I be accessing the object that's returned by getElementsByTagName differently?
Maybe just treating the the XML as data and using jQuery selectors to pull out what you want would work.
$(xml).find("yweather:atmosphere").attr("humidity") - you might need to use filter instead of find - what do you think?
I had the exact same problem, but came across the answer here:
http://reference.sitepoint.com/javascript/Document/getElementsByTagName
It has to do with the yweather namespace. Use getElementByTagNameNS function instead.
var atmosphere = xml.getElementsByTagName("yweather:atmosphere");
becomes
var atmosphere = xml.getElementsByTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0","atmosphere");
function reference:
http://reference.sitepoint.com/javascript/Document/getElementsByTagNameNS
Have you tried atmosphere[1] instead of 0 for Chrome and Safari?