开发者

try to open a page every 10 seconds

开发者 https://www.devze.com 2022-12-17 13:15 出处:网络
Using Javascript (or Ajax) I want to connect to a page (a .php page) every 10 seconds. This will be done in user-s开发者_JAVA技巧ide (browser) within a web page. Just, I\'m trying to see the online us

Using Javascript (or Ajax) I want to connect to a page (a .php page) every 10 seconds. This will be done in user-s开发者_JAVA技巧ide (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.


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.


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>


<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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号