0

I'm looking for the right way to load a random html page (from a folder, which contains all the pages) into a div. This is what I've done for now:

<html>  
<head>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() {
$("#CONTENTS").load("contenuti/1.html"); 
}); 
</script>

</head>  
<body>

<div id="CONTENTS"></div>

</body>
</html>

I think the right way is to transform the "1.html" in "*.html" and then insert a random code. Could anyone help me?

T.C.
  • 129,563
  • 16
  • 274
  • 404

1 Answers1

3

You can do this:

$(function() {
   var max = 10, min = 1;
   $("#CONTENTS").load("contenuti/"+Math.floor(Math.random()*(max-min+1)+min)+".html"); 
});

The above will take random html pages named from 1 to 10.

The random number code courtesy: Francisc's answer to the question Generate random number between two numbers in JavaScript

Community
  • 1
  • 1
Amit Joki
  • 56,285
  • 7
  • 72
  • 91