Javascript Implementation of humanized prompt information on websites

Source: Internet
Author: User

Web2.0 focuses on the spirit of participation of the masses. The success of blog, group, wiki encyclopedia, and RSS aggregation reflects this point. However, to be involved by the masses, practicality, personalization and humanization are particularly important. Sohu blogs have always been more user-friendly. One of them is that the prompt information next to the account after login can be displayed randomly according to the time.

In fact, there are many ways to implement this function. You can use background scripts such as ASP and PHP, or use JavaScript to implement it at the front end, here I mainly analyze the implementation of Sohu.

<Script language = "JavaScript">

// Define the prompt information array. c is the common information, which is the information suitable for display every day. M indicates the information displayed in the morning,

// A indicates the information displayed in the morning, N indicates noon, P indicates afternoon, d Indicates evening, e Indicates evening, l indicates 9-12 o'clock, and W indicates late night.

VaR W = {
C: ['Hey ~ ',' More exercise and less illness ','-_-! ',' Welcome to earth ~ ',' The stock has risen again ~ '],
M: ['Good morning ', 'dawn of thing', 'new Day', 'birds singing ~ ', 'Remember to eat breakfast', 'and there is a congestion :('],
A: [Good morning, busy, busy? ', 'Have a coffee', 'take a break ', 'Hurry up.',],
N: ['good midday ', 'lunch time', 'lunch is full,' Are you not going to eat? ', 'Resting, taking a rest'],
P: ['good afternoon ', 'hot room ~ ',' Is the work finished? ', 'Improve work efficiency', '### something difficult'],
D: ['Good evening ',' there is a traffic jam: (',' Are you not leaving? ',' What do you want to eat? ', 'Buy food later', go home and find a mom '],
E: ['Good evening ',' What do you want to eat? ', 'Go to run' at night,' there is no fish in the shredded fish ', 'Every home, every mom'],
L: ['Good night ',' Are you still asleep? ',' The stars are shining tonight ~ ',' Are you still working? '],
W: ['sleep, work tomorrow ~ ',' You are not fighting alone! ', 'Long night without any slee', 'Wake up ~~ Wake up ~ '],
K: ['good weekend ', 'weekend ^ o ^ sleep late', 'Call your home', 'Should you go out and bask in the sun ', 'invite friends to play play']
};

// Display the corresponding prompt information based on the time.
Function GW (){
VaR T = new date ()
VaR H = T. gethours (); // get the current hour
VaR d = T. getday (); // get the current week
VaR L = W. C; // common information
If (h> = 6 & H <9) {L = L. Concat (W. M);} // display general and morning prompts
If (h> = 9 & H <12) {L = L. Concat (W. a);} // display general and morning prompts
If (H >=12 & H <14) {L = L. Concat (W. N );}
If (H >=14 & H <17) {L = L. Concat (W. P );}
If (H >=17 & H <19) {L = L. Concat (W. D );}
If (H >=19 & H <21) {L = L. Concat (w. e );}
If (H >=21 & H <24) {L = L. Concat (W. L );}
If (h> = 0 & H <6) {L = L. Concat (w. w );}
If (D = 0 | D = 6) {L = L. Concat (W. K);} // a weekend greeting is displayed.
VaR n = math. round (math. random () * L. length); // math. random () * L. length generates a number ranging from 0 to the number of greetings, math. the round () function is used to take an integer.
If (n = 0) {n = 1;} if n is 0, it is set to 1 because the returned value is n-1.
Return (L [n-1]);
}

</SCRIPT>

In this way, you can use HTML to display the value generated by the GW function.

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.