最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

web - How can I count the number of the visitors on my website using JavaScript? - Stack Overflow

programmeradmin2浏览0评论

I need a counter to integrate inside my HTML code that counts from one to three when the visitors visited my webpage.

For example if the first visitor visited my page it count to 1, then the next visitor visited the page it count to 2, and for the 3rd visitor count to 3, then for the 4th visitor again starts from 1 and so on.

I need a counter to integrate inside my HTML code that counts from one to three when the visitors visited my webpage.

For example if the first visitor visited my page it count to 1, then the next visitor visited the page it count to 2, and for the 3rd visitor count to 3, then for the 4th visitor again starts from 1 and so on.

Share Improve this question asked Jul 24, 2015 at 12:50 golagola 331 gold badge1 silver badge3 bronze badges 6
  • 2 You'd need some sort of server side counter for that I should think. – NMunro Commented Jul 24, 2015 at 12:52
  • You should be able to set it so you use AJAX to POST information to a text file. You can load that file on webpage open and increment and send it back, but that would not prevent collisions, so some users might be loading the page at the same time and it would not be guaranteed accurate. – Kenneth Salomon Commented Jul 24, 2015 at 13:02
  • let a fully featured service do it for you google./analytics – dm03514 Commented Jul 24, 2015 at 13:04
  • considering randomness how users visit websites you can very well achieve similar result by generating 3 random numbers. well, in case you don't want any backend solution. – webduvet Commented Jul 24, 2015 at 13:08
  • Well, the reason I am asking this question, is because every time a user visited my webpage I want to show he/she a different website inside my homepage. Therefore, at the end I will be displaying my three websites to my visitors with the same probability. If somebody has a better idea please let me know. – gola Commented Jul 24, 2015 at 13:20
 |  Show 1 more ment

2 Answers 2

Reset to default 2

You could use a cookie and simply update the cookie count detected when the user refreshes, or with a timer. You could also do different things once you have the cookie. You could also take advantage of localstorage.

Check out - http://samy.pl/evercookie/

<script type="text/javascript" src="evercookie.js"></script>

<script>
var ec = new evercookie(); 

// set a cookie "id" to "12345"
// usage: ec.set(key, value)
ec.set("id", "12345"); 

// retrieve a cookie called "id" (simply)
ec.get("id", function(value) { alert("Cookie value is " + value) }); 

// or use a more advanced callback function for getting our cookie
// the cookie value is the first param
// an object containing the different storage methods
// and returned cookie values is the second parameter
function getCookie(best_candidate, all_candidates)
{
    alert("The retrieved cookie is: " + best_candidate + "\n" +
        "You can see what each storage mechanism returned " +
        "by looping through the all_candidates object.");

    for (var item in all_candidates)
        document.write("Storage mechanism " + item +
            " returned: " + all_candidates[item] + "<br>");
}
ec.get("id", getCookie); 

// we look for "candidates" based off the number of "cookies" that
// e back matching since it's possible for mismatching cookies.
// the best candidate is most likely the correct one
</script> 

As indicated in the ments, if you are just looking for some method of loading random pages with equal probability, without more plicated server-side code, you could do something like this:

<script type="text/javascript">
    window.onload = function() {
        var numberOfPages = 3;
        var num = Math.round(Math.random() * numberOfPages);
        window.location.href = "/" + num + ".html";
    };    
</script>

Of course, then you would have pages 0.html, 1.html, and 2.html. This will randomly load one of these three pages with equal probability. It can also be extended to as many pages as you wish, just add more pages and increase the numberOfPages variable. This should work if you put this script within your site's index--the rest of the page can be empty.

发布评论

评论列表(0)

  1. 暂无评论