How to achieve random display of advertisements on Javascript static pages

Source: Internet
Author: User

First, you need to generate random numbers not repeated within n, and put the generated random numbers into another Array. Use the following method to complete the process:
Copy codeThe Code is as follows:
Function getRandomNumbers (count)
{
Var numbers = new Array ();
Var original = new Array ();
For (var I = 0; I <count; I ++)
{
Original [I] = I + 1;
}
Original. sort (function () {return 0.5-Math. random ();});
For (var I = 0; I <count; I ++)
{
Numbers [I] = original [I]-1;
}
Return numbers;
}

Then, in the background program, output the html Script:
Copy codeThe Code is as follows:
Var sf = new Array ();
Sf [0] = '<tr> <TD width = 120> <a href = # target = _ blank> server name </a> </TD> <TD width = 110> <a href = http:// SC .jb51.net target = _ blank> Server IP </a> </TD> <TD width = 160> opening time </TD> <TD align = center width = 95> line Category </TD> <TD> Version introduction </TD> <TD width = 105> customer service QQ </TD> <TD align = center width = 55> <a href = # target = _ blank> <font color = #000000> detailed introduction </font> </a> </TD> </tr> ';
Sf [1] = '<tr> <TD width = 120> <a href = # target = _ blank> server name </a> </TD> <TD width = 110> <a href = http://s.jb51.net target = _ blank> Server IP </a> </TD> <TD width = 160> opening time </TD> <TD align = center width = 95> line Category </TD> <TD> Version introduction </TD> <TD width = 105> customer service QQ </TD> <TD align = center width = 55> <a href = # target = _ blank> <font color = #000000> detailed introduction </font> </a> </TD> </tr> ';

Use the previous method to generate a random number and display the html Script according to the random number.
Copy codeThe Code is as follows:
Var numbers = getRandomNumbers (sf. length );
For (I = 0; I <numbers. length; I ++)
{
Document. write (sf [numbers [I]);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.