killah Posted March 4, 2009 Share Posted March 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/147917-reload-function/ Share on other sites More sharing options...
rhodesa Posted March 4, 2009 Share Posted March 4, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/147917-reload-function/#findComment-776345 Share on other sites More sharing options...
killah Posted March 5, 2009 Author Share Posted March 5, 2009 I would like to actualy use my own code. I tried that one you gave me. It work's fine and all, but it gives about another 0.001 load to the page which i do not like. Quote Link to comment https://forums.phpfreaks.com/topic/147917-reload-function/#findComment-777533 Share on other sites More sharing options...
ann Posted May 5, 2009 Share Posted May 5, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/147917-reload-function/#findComment-826581 Share on other sites More sharing options...
ann Posted May 5, 2009 Share Posted May 5, 2009 Doh! I forgot to put session_start(); at the beginning of test1.php. Sorry to have bothered you. Quote Link to comment https://forums.phpfreaks.com/topic/147917-reload-function/#findComment-826609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.