I’m trying to create a random generator and on another page if found a hint, that this would be easy using jQuery, so I tried the following.
<html>
<head>
<title>hello</title>
</head>
<body>
<script type="text/javascript">
$ (document).ready(function() {
$("body").load("hello.txt", function(msg) {
var textArray = msg.split("\n");
var zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
});
});
document.write('<p>' + textArray[zufall] + '</p>');
</script>
</body>
</html>
it should work like this:
it loads a document with several lines of text ans splits it up at line break. this should be stored in an array and a random line should be displayed on the website.
My first idea was to write the text directly into an array but I thought loading it would be more efficient for the website.
Thanks for answering
PS: there isn’t a error-message like “Error on this page” when the browser runs it.
Final Edit:
Thanks for helping!!!
Now it works.
Here’s the solution:
<html>
<head>
<title>hello</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$ (document).ready(function() {
$.get("hello.txt", function(msg) {
var textArray = msg.split("\n");
var zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
$('body').append('<p>' + textArray[zufall] + '</p>');
});
});
</script>
</body>
</html>
Try this alternate solution
Also make sure you have included the jquery file.