So, I've got this task from school that drives me crazy. I am going to take data from a .dat file which contains this:
812¤SuperIT¤2015-12-06 18:00¤25
614¤MediaHuset¤2016-01-14 16:15¤67
My script works if I replace the "¤" with, for example, ";", but it doesn't work with "¤", "¤
", "¤
"
window.onload = start;
var xhttp;
function start() {
document.getElementById('sub').onclick = load;
}
function load() {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = statusChange;
xhttp.open('GET', '../presentasjoner.dat', true); //GET or POST
xhttp.send();
}
function statusChange() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
var content = xhttp.responseText;
var lines = content.split("\n");
document.getElementById('table').innerHTML = "";
for (var i = 0; i < lines.length; i++) {
var parts = lines[i].split('¤');
document.getElementById('table').innerHTML +=
"<h4>" + parts[0] + "</h4>" +
parts[1] + "<br/>" +
"Antall plasser: " + parts[2] + "<br/>";
}
}
}
The HTML looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Oppgave 1 - Oblig 5</title>
<link rel="stylesheet" href="../css/common.css">
<link rel="stylesheet" href="../css/common.css">
<script type="text/javascript" src="../js/oppg1.js"></script>
</head>
<body>
<div class="commonDiv">
<a href="http://ask.hiof.no/~joakimsg/GRIT/WEB/html/fanpage.html"><h1>Oblig 4, Joakim Granaas</h1></a><br/>
<h3>Oppgave 1</h3><br/>
<input id ="sub" type ="submit" /><br/>
<div id="table"></div>
</div>
</body>
</html>
Splitting by "¤" def. works, see here: https://jsfiddle.net/s7t0c5jg/ --- so the issue is either elsewhere in your code OR your browser has a hard time processing the "¤" or you are saving your file as UTF8 or something and "¤" could be a higher character than just UTF8?
After reading the comments above I think Axel is correct in saying "Usually ¤ just only indicates a non printable character and it does not mean that this char is actually within your data. Open the data file with an hex editor and have a look at the very separator ascii value"
JSFiddle source:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">split by "¤"</button>
<p id="result"></p>
<script>
function myFunction() {
var str = "Item1 ¤ Item2 ¤ Item3 ¤ Item4";
var res = str.split("¤");
document.getElementById("result").innerHTML = res;
}
</script>
</body>
</html>