Blog Tips: use JavaScript to create a counter for your blog

Source: Internet
Author: User

 

In the past, csdn blogs had no counting function, so they were always using others' counters, but this was not accurate. now the csdn blog has the counter function, so I made a counter using JavaScript + Div.

Basic Principles: The getelementsbytagname function retrieves the number accessed on the page and converts it to an image.

Study the HTML code

<H1> blog Statistics <Ul class = "list">
<Li class = "listitem"> original-75 </LI>
<Li class = "listitem"> translation-0 </LI>
<Li class = "listitem"> posting-5 </LI>
<Li class = "listitem"> click-351278 </LI>
<Li class = "listitem"> comment-891 </LI>
<Li class = "listitem"> trackbacks-41 </LI>
</Ul>

 

I use the Li label here. You may have different labels, but the principles are the same.

Add a div to display the counter.

<P> <B> access statistics </B> </P> <Div id = counter> </div>

 

 

JavaScript code

<Script language = JavaScript>

VaR OBJ = Document. getelementsbytagname ('lil ');

VaR CNT, I;

VaR STR, html = '';

For (CNT = 0; CNT <obj. length; CNT ++)

{

STR = OBJ [CNT]. innerhtml;

If (Str. indexof ('click ')! =-1)

{

STR = Str. substr (5 );

Html = "<Table border = '0'> <tr> <TD bgcolor = '#000000'> ";

For (I = 0; I <Str. length; I ++)

If (Str. substr (I, 1)> = '0' & Str. substr (I, 1) <= '9 ')

HTML + = " ";

HTML + = "</TD> </tr> </table> ";

Document. All ['counter']. innerhtml = HTML;

Break;

}

}

</SCRIPT>

This is my counter.

 

In addition, the above code is for a lighty-style blog, so if your blog is not of this style, it is invalid even if you use this code. At the request of aeonspider, I slightly changed the code and used the following code to select a cogitation blog. You can also implement the counter function.

<P> <B> access statistics </B> </P> <Div id = counter> </div>

 

 

<Script language = JavaScript>

VaR OBJ = Document. getelementsbytagname ('td ');

VaR CNT, I;

VaR STR, html = '';

For (CNT = 0; CNT <obj. length; CNT ++)

{

STR = OBJ [CNT]. innerhtml;

If (Str. indexof ('Next click ')! =-1)

{

Pos = Str. indexof ('click ');

Pos-= 1;

Html = "<Table border = '0'> <tr> <TD bgcolor = '#000000'> ";

 

For (I = Pos; I> 0; I --)

{

If (Str. substr (I, 1)> = '0' & Str. substr (I, 1) <= '9 ')

Html = " "+ HTML;

Else

Break;

}

HTML + = "</TD> </tr> </table> ";

Document. All ['counter']. innerhtml = HTML;

Break;

}

}

</SCRIPT>

 

 

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.