Welcome to WebmasterWorld Guest from 107.23.37.199

Forum Moderators: open

Message Too Old, No Replies

Loop works in Firefox but not IE

     
5:37 pm on Apr 6, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0


First of all, i do not really know JavaScript...but have put together a very basic script that is supposed to essentially ping a specific URL every couple minutes. It works perfectly in Firefox, but in Internet Explorer it seems to only run once when the page first loads, rather than in a continuous loop. I thought that JavaScript was pretty standard across browsers? Any help is greatly appreciated (not just to fix this issue, but to do this in a better way as well). THANKS!

Here is my HTML page:
<html>
<head>
</head>
<body>
<p>some unrelated stuff here</p>
<script type='text/javascript' src="http://www.domain.com/sample/heartbeat.js"></script>
<script type="text/javascript">
var userid = "test";
heartbeat();
</script>
</body>
</html>


Here is the heartbeat.js file:

function heartbeat()
{
// send a heartbeat every 4.6 minutes (280000 milliseconds)
setTimeout("heartbeat()",280000);
SendPulse();
}

var req;
function SendPulse()
{
var url = "http://www.domain.com/sample/checkin.php?id=" + userid ;
if (typeof XMLHttpRequest != "undefined")
{
req = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, false);
req.send(null);
}


I have also tried adding " return true; " as the last line in both functions, and while it continued to work in FF, it did not fix the IE problem.
7:24 pm on Apr 7, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Try this. Untested, but you're missing the attempt on the Msxml2 object.


window.onload= function() { heartbeat('test'); };
//
function heartbeat(userid) {
// send a heartbeat every 4.6 minutes (280000 milliseconds)
setTimeout("heartbeat(userid)",280000);
var url = "http://www.domain.com/sample/checkin.php?id=" + userid ;
SendPulse(url);
}
//
function SendPulse(url) {
var req = GetXmlHttpObject();
if (req) {
req.open("GET", url, false);
req.send(null);
}
else { alert('XMLHTTP object not defined in this browser'); }
}
//
function GetXmlHttpObject() {
var xmlHttp=null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) {
//Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
3:15 am on Apr 8, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


As you don't appear to be interested in the pulse reply, you could simplify it to

function heartbeat()
{
// send a heartbeat every 4.6 minutes (280000 milliseconds)
setTimeout("heartbeat()",280000);
document.createElement('iframe').src = "http://www.domain.com/sample/checkin.php?id=" + userid ;
}
3:15 am on Apr 8, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts:91
votes: 0


I was able to follow the logic in your code, but for some reason the problem is now worse :-(

In Firefox it does ping the server once (upon initial page load)...so i know it is sort of working. After that, no further updates (no looping it appears). Internet Explorer does nothing (even on page load). Now i am really confused... I am going to try merging your other type of xmlHttp object with my original code and see what happens there... I will post back in a little while.
3:34 am on Apr 8, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0


So i tried this composite of my original and your new function which got Firefox working again, but still nothing from IE:

HTML:

var userid = "test";
heartbeat();



JavaScript included file:

function heartbeat()
{
// send a heartbeat every 4.6 minutes (280000 milliseconds)
setTimeout("heartbeat()",280000);
SendPulse();
}

var req;
function SendPulse()
{
var url = "http://www.domain.com/sample/checkin.php?id=" + userid ;


try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

xmlHttp.open("GET", url, false);
xmlHttp.send(null);
return true;
}
6:46 pm on Apr 19, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0


Any other suggestions anyone? I am still lost. Thanks!
12:43 am on Apr 20, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


You send the request but do not wait for a reply, this may be the issue ?

Try adding code to read reply, or try my prior post.
4:05 am on Apr 20, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0


Hi daveVk... I completely missed your post on the 7th, as the two of us posted at the exact same minute (what are the odds?!?) Anyway, i tried that, but could not get it to work in either FF or IE...so after poking around on Google I learned that there is some sort of problem in IE with not being able to create iframes. I found a workaround on this page: [msdn.microsoft.com...]


My final (working) code looks like this for anyone who is trying to do something similar:

// send a heartbeat every 4.6 minutes (280000 milliseconds)
setTimeout("heartbeat()",280000);
var rem = document.createElement("div");
rem.innerHTML = '<iframe style="display:none" src="http://www.example.com/sample/checkin.php?id=' + userid +'" id="rpc" name="rpc"></iframe>';
document.body.appendChild(rem);



FINAL QUESTION: Is there any realistic problem to doing it this way? I expect this page to keep running this indefinitely...nothing is going to start stacking up to the point of causing a problem...right? Thanks!

[edited by: Fotiman at 2:26 pm (utc) on Apr 20, 2010]
[edit reason] Examplified URL [/edit]

5:05 am on Apr 20, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


Ok, did not realize problem with creating iframes.

Check if it still works with this line removed

document.body.appendChild(rem);

As you don't want to add divs to document indefinitely

An alternative would be declare an iframe at say the bottom of the page

<iframe id="myIframe" style="display:none" />

js becomes

setTimeout("heartbeat()",280000);
document.getElementById("myIFrame").src = "http://www.example.com/sample/checkin.php?id=' + userid +'" id="rpc" name="rpc"';

[edited by: Fotiman at 2:27 pm (utc) on Apr 20, 2010]
[edit reason] Examplified URL [/edit]