Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try to open a page every 10 seconds

Using Javascript (or Ajax) I want to connect to a page (a .php page) every 10 seconds. This will be done in user-side (browser) within a web page. Just, I'm trying to see the online users. I have about 1-2 visitors daily, in my personal web site.

like image 824
ilhan Avatar asked Jan 15 '10 06:01

ilhan


3 Answers

Using jQuery's $.post() method:

setInterval(function(){
  $.post("getCount.php", function(result) {
    // do something with result
  }, "html");
}, 10000);

I'm assuming you have a good reason to query your own local script. If you want detailed information about who is visiting your site, when, and from what types of environments (machines, browsers, etc), I'd suggest you look into implementing something like Google Analytics.

like image 154
Sampson Avatar answered Nov 17 '22 20:11

Sampson


This Javascript will read the page usersonline.php every 10 seconds and place the contents onto the current web page.

<html>
<head>
<script>

var xmlrequest;

function gotnewdata()
{
    if(xmlrequest.readyState == 4)
    {
        document.getElementById("output").innerHTML = xmlrequest.responseText;
        setTimeout("loadpage();", 10000);
    }   
}

function loadpage()
{
    xmlrequest = new XMLHttpRequest();
    xmlrequest.open("GET", "usersonline.php", true);
    xmlrequest.onreadystatechange = gotnewdata;
    xmlrequest.send(null);
}

</script>
</head>
<body onload="loadpage();">
<h1>My Page</h1>
<p>USERS ONLINE:</p><p id="output"></p>
</body></html>
like image 1
Adam Pierce Avatar answered Nov 17 '22 19:11

Adam Pierce


<html>
<body>
<form target='userCountFrame' action='http://www.google.com'></form>
<iframe name='userCountFrame'></iframe>
<script>
setInterval(function(){
  document.getElementsByTagName('form')[0].submit();
}, 10 * 60 * 1000);
</script>
</body>
</html>

change the url accordingly, save the above code as count.html on your desktop, and open it using Firefox

like image 1
Dapeng Avatar answered Nov 17 '22 19:11

Dapeng