How to achieve random display of advertisements on Javascript static pages _ javascript skills

Source: Internet
Author: User
Recently, when publishing a private server, the customer requested random ad sorting and implementation on the html page, that is, javascript must be used. 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:

The Code is as follows:


Function getRandomNumbers (count)
{
Var numbers = new Array ();
Var original = new Array ();
For (var I = 0; I
{
Original [I] = I + 1;
}
Original. sort (function () {return 0.5-Math. random ();});
For (var I = 0; I
{
Numbers [I] = original [I]-1;
}
Return numbers;
}


Then, in the background program, output the html Script:

The Code is as follows:


Var sf = new Array ();
Sf [0] ='Server NameServer IP AddressOpening TimeLine TypeVersion IntroductionCustomer service QQDetails';
Sf [1] ='Server NameServer IP AddressOpening TimeLine TypeVersion IntroductionCustomer service QQDetails';


Use the previous method to generate a random number and display the html Script according to the random number.

The Code is as follows:


Var numbers = getRandomNumbers (sf. length );
For (I = 0; I {
Document. write (sf [numbers [I]);
}

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.