Thursday, March 6, 2014

div2.php repeated the words,

different refresh divs with javascript and ajax 'Vcomputadoras.com
In a previous POST leave them a tutorial on how to refresh a div tag without refreshing the entire page, well this time and better polished code e created a function to refresh any number of DIVs without the whole page is refreshed. . This facilitates the use of the code for any circumstance we face in creating our web .. example for how to use Ajax with a Mysql database handles data with Ajax Mysql
The function we use is called refreshDivs (divid secs, url) function accepts 3 parameters leaders: divid = this is the ID of the div you want to refresh. secs = the second you want the div is not cool. url = The address of the document you want to appear on the DIV.
Ok now the next step is to place in your html between "<head>" tags the following code shown below. stamp font <script type="text/javascript" language="javascript"> refreshDivs function (divid, secs, url) {/ / define our vars var divid secs, url, fetch_unix_timestamp / / We check that the variables are empty .. if (divid == "") {alert ('Error: type id of the div you want to refresh') return;} else if {alert ('Error (document.getElementById (divid)!): The Div selectionado no ID is defined: '+ divid) return;} else if (secs == "") {alert (' Error: Indicates the number of seconds you want the div to cool ') return;} else if (url == "") {alert ('Error:. URL of the document you want to load into the div can not be empty') return;} / / The XMLHttpRequest stamp font object var xmlHttp try {xmlHttp = new XMLHttpRequest () / / Firefox , Opera 8.0 +, Safari} catch (e) {try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP") / / Internet Explorer} catch (e) {try {xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); stamp font } catch (e) {alert ("Your browser stamp font does not support AJAX.") return false;}}} / / Timestamp to prevent the GET array fetch_unix_timestamp stamp font = function () {return parseInt is to cache (new Date () getTime. () toString () substring (0, 10))} var timestamp = fetch_unix_timestamp ().. nocacheurl var timestamp = url + + "t ="; / / the ajax call xmlHttp.onreadystatechange = function () {if (xmlHttp . readyState == 4 && xmlHttp.status == 200) {document.getElementById (divid) innerHTML = xmlHttp.responseText,. setTimeout (function () {refreshDivs (divid secs, url);} secs * 1000);} Xmlhttp.open} ("GET", nocacheurl, true); xmlhttp.send (null);} / / call functions with parameters repectivos we want to refresh the DIVs. startrefresh window.onload = function () {refreshDivs ('div1', 5, 'div1.php'); refreshDivs ('div2', 3, 'div2.php'); refreshDivs ('div3', 10, 'div3. php ');} </ script>
The part of the code where we indicated stamp font that we must refresh divs is: startrefresh window.onload = function () {refreshDivs ('div1', 5, 'div1.php'); refreshDivs ('div2', 3, 'div2. php '); refreshDivs (' div3 ', 10,' div3.php ');}
Using window.onload event we run our browser refresh function and this divs that we want them to be refreshed stamp font without reloading or refreshing the whole page .. Repeat with different functions div, urls times and there are no limitations to the amount of time you want to use it.
This document will be called every 5 seconds and will be shown on the div1 The other two examples are similar to the first, only change the ID of the DIV the time in seconds and the URL or address of the PHP document. <? Php echo "Here stamp font we get numbers <p style=\"background-color:#FF7400\"> grilling" rand (1, 1000) "</ p>",..> div2.php
div2.php repeated the words, "Please repeat me" as many times as the variable $ times as defined by the function rand (). ? <Php $ time = rand (1.20) for ($ i = 0, $ i <= $ times, $ i + +) {echo "<p style=\"background-color:#356AA0\">". . $ i "Please repeat me," stamp font $ time "times </ p>.",..}> div3.php?
The same is true for div3.php, stamp font repeated the words "Please repeat me" as many times as the variable $ times as defined by the function rand (), with the only difference that the color of the background is different and the amount seconds to refresh ... <php $ time = rand (1.20) for ($ i = 0, $ i <= $ times, $ i + +) {echo "<p style = \" background-color: # C79810 .. \ ">" $ i "Please repeat me," $ time "times </ p>.",..?}>
Not only can refresh with Ajax divs but also other html tags such as span, form, tables, H, P and others, you just have to define an id for that tag, an important note is that the document is called stamp font must run or parse the content on your server, because Ajax is in the Customer will

No comments:

Post a Comment