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.
  • 133,968
  • 17
  • 288
  • 421

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
  • 58,320
  • 7
  • 77
  • 95
  • fwiw `var max = 10, min = 1;Math.random()*(max-min+1)+min` may return `8.37721847370267` . `Math.floor(Math.random()*(max-min+1)+min)` to return `8` ? – guest271314 Jun 22 '14 at 14:40