Jump to content

Reload Function


killah

Recommended Posts

Hi, i attempted to create this javascript code with a bit of help from some where. Forgot now. Well, i had it all in one big jumble so i had to basicly add like 30 lines to where ever i wanted to use the code. So i decided what if i can add it into a .js document and add a function all around it with the time interval, page, and div id.

 

This is my javascript code:

 

function reload(page,time,div_id)
{
var xmlHttp; 
function GetXmlHttpObject()
{ 
	var objXMLHttp=null; 
	if (window.XMLHttpRequest)
	{ 
		objXMLHttp=new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject)
	{ 
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	return objXMLHttp; 
} 
function update(page_u,div_id_u)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 && xmlHttp.status == 200)
		{
			var response = xmlHttp.responseText;
			if (response)
			{
				document.getElementById(div_id_u).innerHTML=response;
			}
		}
	}
	var url=page_u;
	xmlHttp.open("get", url+ "?ms=" + new Date().getTime());
	xmlHttp.send(null);
}
setInterval("update(page,div_id)", time);
}

 

And this is the html code i'm trying to use.

<html>
<head>
	<title>Testing Javascript</title>
	<script type="text/javascript" src="scripts/reload.js"></script>
</head>
<body>
	<script>reload('test1.php',1000,'time');</script>
	<div id="time" style="border: 1px black solid; height: 150px; width: 150px;"></div>
</body>
</html>

 

Inside test1.php i had a rand format that is just displaying a rand number every load. But nothing get's outputted.

 

Any ideas?

Link to comment
Share on other sites

your problem most likely stems from variable scope and nested function issues. things aren't as simple as 'putting it inside a function'. my suggestion is to save your time for more important stuff and just use jQuery. it will handle all of this for you:

<html>
   <head>
      <title>Testing Javascript</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
      <script type="text/javascript">
        $(function(){
          //This code will run once the DOM is done loading
          setInterval("$('#time').load('test1.php');",1000);
        });
      </script>
   </head>
   <body>
      <div id="time" style="border: 1px black solid; height: 150px; width: 150px;"></div>
   </body>
</html>

Link to comment
Share on other sites

  • 1 month later...

rhodesa

 

I want to post variables from a form to 'test1.php' via your script.  I thought I'd set the variables as session cookies at the start of your script but there are no session cookies when test1.php runs.  It's probably the wrong approach anyway can you advise on the correct way to do this?

 

Thanks

 

My version of your script which reports that session cookies but test1.php doesn't

<?
session_start();  // Start Session 
session_register('analysis'); 
$_SESSION['analysis'] = $_REQUEST['analysis'];
session_register('query'); 
$_SESSION['query'] = $_REQUEST['query'];
?>

<html>
   <head>
      <title>Testing Javascript</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
      <script type="text/javascript">
        $(function(){
          //This code will run once the DOM is done loading
          setInterval("$('#time').load('test1.php');",1000);
        });
      </script>
   </head>
   <body>
      <div id="time" style="border: 1px black solid; width: 750px;"></div>
   <? 
      echo $_SESSION['analysis']." analysis<br>";
      echo $_SESSION['query']." query<br>";
   ?>
   </body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.