LeFizzy LeFizzy - 4 months ago 12
HTML Question

jQuery - Random image generator (fix my code,maybe only syntax errors)

Welcome again,I have some problems with my code.
I still want to get random() background images,but
I can't get it working. It show the default white background with the two text 'title'. :(
My files are => in extension '.jpg' and in directory '/res/images' what's wrong?



<!doctype html>
<html>
<head>
<title>Random_page</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>

<script>
$(document).ready(function(){

//the highest number of the image you want to load
var upperLimit = 10;

//get random number between 1 and 10
//change upperlimit above to increase or
//decrease range
var randomNum = Math.floor((Math.random() * upperLimit) + 1);


//change the background image to a random jpg
//edit add closing ) to prevent syntax error
$("body").css("background-image","url('res/images/" + randomNum + ".jpg')");//<--changed path

});
</script>
</head>
<body>
<div id="container1" style="float:left; width:40%; margin:10px;">
<h1>Title</h1>
<img src="" width='100%' height='100%' />
</div>
<div id="container2" style="float:left; width:40%; margin: 10px;">
<h1>Title</h1>
<img src="" width='100%' height='100%' />
</div>
</body>
</html>




Answer

Developer console says:

ReferenceError: $ is not defined.

It means jqery didn't load. Actual reason is missing http:

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>