Javascript code reads XML local file into array variable arrTst. It works in Firefox 25.0.1 and I can see arrTst values in debugger. I have problems working with this arrTst. Example is displaying alert.
If I swap alert lines (displaying arrTest first) then alerts are not displayed.
Can anyone explain why alerts are not displayed when lines are swapped ?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<inData>
<record>
<date>2012-08-01</date>
<amount>7</amount>
</record>
<record>
<date>2012-08-02</date>
<amount>22</amount>
</record>
</inData>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<script type="text/javascript" src="jquery-1.10.2.js"> </script>
<title>test read XML file</title>
</head>
<body>
<h1 >Test XML</h1>
<div id="TstMsgArea"></div>
<script type="text/javascript">
var str1="";
var arrTst=[];
var arrCon=[["2013-08-01", 1], ["2013-08-02",33]];
$(document).ready(function(){
/*******************************************************************
* read xml file from local filesystem & push to array
*******************************************************************/
$.get("test.xml",{},function(xml){
$('record',xml).each(function(i) {
_date = $(this).find("date").text();
_amount = parseInt($(this).find("amount").text());
var X0=[];
X0.push(_date);
X0.push(_amount);
arrTst.push(X0);
alert (str1);
str1+=_date +", "+_amount+"; ";
$("#TstMsgArea").append(str1);
});
});
/*******************************************************************
* check array values - if lines swapped no go !
*******************************************************************/
alert ("arrConst : " + arrCon[0][0]+", " + arrCon[0][1]+"; " + arrCon[1][0]+", " + arrCon[1][1]);
alert ("arrTest : " + arrTst[0][0]+", " + arrTst[0][1]+"; " + arrTst[1][0]+", " + arrTst[1][1]);
}); //$(document).ready
</script>
</body>
</html>
Your alerts are in the wrong place. You need to put them inside your success function, or in another function that you call from within that function.
Also, you shouldn't declaring str1 and arrTst outside the success function. Those are values you are building up inside that function, and they won't be available outside of it.
If you want to use those variables somewhere else in your code, you need to put the code that uses them inside a function somewhere. Call that function from inside your $.get() success function, and pass it the variables as an argument. Then you can be sure that the data is ready.
var arrCon=[["2013-08-01", 1], ["2013-08-02",33]];
$(document).ready(function(){
/*******************************************************************
* read xml file from local filesystem & push to array
*******************************************************************/
$.get("test.xml",{},function(xml){
var str1="";
var arrTst=[];
$('record',xml).each(function(i) {
_date = $(this).find("date").text();
_amount = parseInt($(this).find("amount").text());
var X0=[];
X0.push(_date);
X0.push(_amount);
arrTst.push(X0);
alert (str1);
str1+=_date +", "+_amount+"; ";
$("#TstMsgArea").append(str1);
});
alert ("arrConst : " + arrCon[0][0]+", " + arrCon[0][1]+"; " + arrCon[1][0]+", " + arrCon[1][1]);
alert ("arrTest : " + arrTst[0][0]+", " + arrTst[0][1]+"; " + arrTst[1][0]+", " + arrTst[1][1]);
});
}); //$(document).ready
Related
I am newbie with javascript and I wrote a very simple program to display something using console command like this
function dogYears(dogName, age) {
var years = age * 7;
console.log(dogName + " is " + years + " years old");
}
var myDog = "Fido";
dogYears(myDog, 4);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Exercise2</title>
</head>
<body>
</body>
</html>
As you can see, I hope that I made the function and then, use var to assigned the value myDog to Fido, and then I want to use the function dogYears to print by the command console.log.
But when I ran the file .html (I saved it by .html), it did not display anything.
What error did I get in this case ? Could you please help me in this one ? Thank you very much for your help.
To display your result in browser's console just add your JavaScript logic inside script tag in your html file before closing body tag or create separate JavaScript file and import it using script tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Exercise2</title>
</head>
<body>
<script>
function dogYears(dogName, age) {
var years = age * 7;
console.log(dogName + " is " + years + " years old");
}
var myDog = "Fido";
dogYears(myDog, 4);
</script>
</body>
</html>
The problem is that you are not viewing the console. The console is not on the webpage, but instead on a separate tab. Press F12 on the keyboard, or press Ctrl+Shift+i and then click on the console. There is nothing wrong with the code. But if you want a more protected function, you can use this JavaScript code.
<script>
function dogYears(dogName, age) {
if(typeof(dogName)==="string"&&typeof(age)==="number"){
var years = age * 7;
console.log(dogName + " is " + years.toString() + " years old");
}else{
console.log("Error you want to display");
}
}
</script>
If you want to display information on the browser, typed document.write instead of console.log
<script>
function dogYears(dogName, age) {
var years = age * 7;
document.write(dogName + " is " + years + " years old");
}
var myDog = "Fido";
dogYears(myDog, 4);
</script>
I am trying to load an XML file from a HTTP site (will be in the same network) all works fine loading the file in the same folder (as in code part Connect.open("GET", "REPORT.XML", false); ) I tried both true and false and it still doesn't work. What am I missing? As the code below will show undefined for all variable when trying to load anything else than the REPORT.XML not using a http version.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Consommation</title>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 15 (filtered)">
<style>
<!--
/* Font Definitions */
#font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
#font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:0in;
margin-right:0in;
margin-bottom:10.0pt;
margin-left:0in;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{font-family:"Calibri",sans-serif;}
.MsoPapDefault
{margin-bottom:10.0pt;
line-height:115%;}
#page WordSection1
{size:11.0in 8.5in;
margin:42.55pt 1.0in 35.45pt 1.0in;}
div.WordSection1
{page:WordSection1;}
-->
</style>
<script language="JavaScript">
// Create a connection to the file.
var Connect = new XMLHttpRequest();
// Define which file to open and
// send the request.
//Connect.open("GET", "REPORT.XML", false); // THIS WORKS
//Connect.open("GET", "http://192.168.40.15/REPORT.XML", false); // THIS DOES NOT WORK
Connect.open("GET", "http://127.0.0.1/REPORT.XML", false); // THIS DOES NOT WORK
Connect.setRequestHeader("Content-Type", "text/xml");
Connect.send(null);
// Place the response in an XML document.
var TheDocument = Connect.responseXML;
// Place the root node in an element.
var Data = TheDocument.childNodes[0];
//document.write(Data.getElementsByTagName("Name")[2].textContent + " of value of " + Data.getElementsByTagName("ActiveValue")[2].textContent + " " + Data.getElementsByTagName("ActiveUnit")[2].textContent + "<br>");
//document.write(Data.getElementsByTagName("Name")[6].textContent + " of value of " + Data.getElementsByTagName("ActiveValue")[6].textContent + " " + Data.getElementsByTagName("ActiveUnit")[6].textContent + "<br>");
//document.write(Data.getElementsByTagName("Name")[10].textContent + " of value of " + Data.getElementsByTagName("ActiveValue")[10].textContent + " " + Data.getElementsByTagName("ActiveUnit")[10].textContent + "<br>");
var insta = Data.getElementsByTagName("ActiveValue")[2].textContent;
var jourcour = Data.getElementsByTagName("ActiveValue")[6].textContent;
var totalvieille = Data.getElementsByTagName("ActiveValue")[10].textContent;
</script>
</head>
<body>
<span lang=FR style='font-size:18.0pt;line-height:115%;color:red'><script language="JavaScript">document.write(insta);</script></span>
<span lang=FR style='font-size:18.0pt;line-height:115%;color:red'><script language="JavaScript">document.write(jourcour);</script></span>
<span lang=FR style='font-size:18.0pt;line-height:115%;color:red'><script language="JavaScript">document.write(totalvieille);</script></span>
</body>
I want to alert "running function cannons" then when I press ok I want it just to say "cannon ship sails off to 14 degrees" but it keeps printing the alert within my output.
JS
function alertMessage (message) {
alert (message);
}
alertMessage("the Battle has begun");
function alertShip (ship, number) {
alert (ship);
document.write (ship + "the ship sails off to " + number + "degrees");
}
alertShip("running function cannons", 14);
HTML
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Functions</title>
<!-- This links to the js code specific for this page -->
<script src="functions.js"></script>
</head>
<body>
<div id="output"> </div>
<div id="output2"> </div>
</body>
</html>
Try removing the ship parameter from the document.write and replacing the statement with this:
document.write("cannon ship sails off to " + number + " degrees");
Then, you should be able to get your desired output.
*p.s.: if you're doing this on a text editor and trying to run this on a browser, you can consider changing the tag to this: .
I hope this helps!
don't give them the same variable to output .. alert (ship) then document.write (ship+.....).
Try this instead
function alertShip (ship, number) {
alert (ship);
document.write ("Cannon ship sails off to " + number + "degrees");
i had a problem .... i want to create HTML/javascript file that see all files in this folder ( in same computer ) , and put the the link each one of them .
i couldn't fine any way to do it , need help on this for real
<HTML>
<HEAD>
<TITLE></TITLE>
<BODY topmargin="0" leftmargin="0">
<div>The current directory is: <script type="text/javascript">
function disp_current_directory(){
var dirs=window.location.href.split('/'),
cdir=dirs[dirs.length-2];
document.write(cdir + " " + window.location.href);
var dirFiles ;
for( int i = 0 , i > dirs.length , i++)
{ dirFiles[i] = ??? }
var txt = window.location.href - dirs[dirs.length-1] ;
document.write( "<br/> " + txt );
}
disp_current_directory();
</script></div>
</BODY>
</HTML>
i don't know what to do .. need help pls
Because of some security issues, JavaScript is not allowed to have access to client files without the user's explicit authorization. What you are trying to achieve can't be done using JavaScript
So I'm trying to display the results of a few function calls in a JavaScript file that uses benchmark.js in a separate HTML file.
My js file looks something like this (disregard the names of methods and classes):
class.init(function(context) {
Benchmark("function description", {
'defer': true,
fn': function(deferred) {
var x = context.ones([100, 100]);
var y = x.repeat(2, 0);
context.barrier(function (){
deferred.resolve();
});
},
'onComplete': function(event) {
//This is what I'd like to print out
console.log(this.name + ": " + (this.stats.mean * 1000).toFixed(2) + " ms");
//
}
}).run();
There are multiple function calls similar to this.
My HTML just looks something lile:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Sublime Webpage </title>
</head>
<body>
<div class="main">
<div class="head">
<h1>Hello</h1>
</div>
<script src="filename.js"></script>
</div>
</body>
</html>
At the moment, it just prints the results to the console, which is better than nothing, but obviously not what I want. I talked to someone briefly and they suggested I use jQuery. I looked into it, and tried using document.write(this.name + ": " + (this.stats.mean * 1000).toFixed(2) + " ms") in the place of console.log(), but this didn't seem to work. Does anyone have suggestions?
Use document.createTextNode:
// add an output div to your html
<div id='output'></div>
// in your benchmark code
var output = document.getElementById('output');
output.appendChild(document.createTextNode('some result'));
Fiddle