Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
..
I have MCU application with http server.
On my static page I need dynamicly updated DIV field.
The upadate should be done from different URL then page.
This URL has a simple TXT format, just a pure numbers,
but updated 4-5 per sec. There are several such sources.


Like on page below, just URL content instead "update #" value.


HTML
<html>   
<head>   
<script langauge="javascript">   
        var counter = 0;   
        window.setInterval("refreshDiv()", 250);   
        function refreshDiv(){   
                counter = counter + 1;   
                document.getElementById("refreshme").innerHTML = "update  " + counter;   
        }   
</script>   
</head>   
<body>   
<div id="refreshme">Update</div>
</body>   
</html>   



Is it possible do without JQuery or any other plugins
Posted
Updated 2-Apr-22 23:03pm
v3
Comments
Vyacheslav Voronenko 1-Aug-13 1:54am    
What have you tried so far? There is a number of examples over the net.


<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/jquery-ui.min.js"></script>
<script src="../Scripts/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="../Scripts/jquery.timers-1.0.0.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () {
var j = jQuery.noConflict();
j(document).ready(function () {
j(".refreshMe").everyTime(5000, function (i) {
j.ajax({
url: "refresh.html",
cache: false,
success: function (html) {
j(".refreshMe").html(html);
}
})
})
});
j('.refreshMe').css({ color: "red" });
});
</script>
mysticaldragon 7-Nov-17 14:00pm    
xzxc
mysticaldragon 7-Nov-17 14:00pm    
zxczxcc

Whenever I want to do something like that, I just use one of the following 3 functions, along with the AjaxRequest library, found here

JavaScript
function myGetAjaxResponse(target, url)
{
  AjaxRequest.get(
    {
      'url':url,
      'onSuccess':function(req){ target.innerHTML=req.responseText; }
    }
  );
}


function myGetAjaxResponseWithCallback(target, url, callbackFunc)
{
  AjaxRequest.get(
    {
      'url':url,
      'onSuccess':function(req){ target.innerHTML=req.responseText; callbackFunc();}
    }
  );
}


function myGetAjaxResponseWithCallback2(target, url, callbackFunc)
{
  AjaxRequest.get(
    {
      'url':url,
      'onSuccess':function(req){ callbackFunc(req.responseText, target);}
    }
  );
}



Here's a quick example of use. You'll just need to get the AjaxRequest library.

1. The php file that returns data we'd like to make use of

jsonDir.php
PHP
<?php
class mFile
{
    public $name, $time, $size;
}

foreach (glob("*.*") as $curFilename)
{
    $curFileObj = new mFile;
    $curFileObj->name = $curFilename;
    $curFileObj->time = date("d/m/Y - H:i", filectime($curFilename));
    $curFileObj->size = filesize($curFilename);
    $fileArray[] = $curFileObj;
}
printf("%s", json_encode($fileArray));
?>


2. The file that makes the request for the data and then does something with it. (I just add the names of all the files in the current directory to a drop-down list - a select element)
index.html
HTML
<!DOCTYPE html>
<html>
<head>
<script src='script/ajaxRequest.js'></script>
<script>
function byId(e){return document.getElementById(e);}
function newEl(tag){return document.createElement(tag);}
function newTxt(txt){return document.createTextNode(txt);}

window.addEventListener('load', mInit, false);

function mInit()
{
}

function myGetAjaxResponseWithCallback2(target, url, callbackFunc)
{
  AjaxRequest.get(
    {
      'url':url,
      'onSuccess':function(req){ callbackFunc(req.responseText, target);}
    }
  );
}

function onLoadBtnClick()
{
    var tgt = byId('resultsContainer');
    myGetAjaxResponseWithCallback2(tgt, 'jsonDir.php', jsonDataAvailableCallback);
}

function jsonDataAvailableCallback(responseText, targetContainer)
{
    var resultArray = JSON.parse(responseText);
    var select, option, i, n, curFilename;

    select = newEl('select');

    n = resultArray.length;
    for (i=0; i<n; i++)
    {
        option = newEl('option');
        option.setAttribute('value', i);        // set the value to be the files index in the array
        curFilename = resultArray[i].name;
        option.appendChild( newTxt(curFilename) );
        select.appendChild(option);
    }
    targetContainer.innerHTML = '';
    targetContainer.appendChild(select);
}

</script>
<style>
</style>
</head>
<body>
    <input type='button' value='load file list' onclick='onLoadBtnClick()'/>
    <div id='resultsContainer'></div>
</body>
</html>
 
Share this answer
 
v2
Comments
Gobsek 1-Aug-13 5:36am    
Sorry, cant understand this or see solution.
enhzflep 1-Aug-13 6:15am    
Okay, let me explain a little more.

1) Visit the link I left (click the word 'here' - the last word before the code block)
2) Select 'Source' from the menu on the left.
3) Download the library
4) Add <script src='ajaxRequest.js'></script> to your html
5) Use one of the functions above - I suggest the first
6) Call the function with
■ (a) the results of document.getElementById('insert_id_of_target_container_here')
■ (b) the url of the html you'd like to retrieve

You can also visit this link to go directly to the download page of the AjaxRequest library.
AjaxRequest > Soure

I'll update my solution to provide an example of use.
Gobsek 2-Aug-13 3:47am    
Sorry, I dont have PHP running on server side.
There are running a simple http daemon (server),
on different platform then Intel or other known.

I need on my page just load into DIV content of another URL,
with the some simple javascript action, so it have to work on any platform
enhzflep 2-Aug-13 4:02am    
What about an iframe? None of my answers have focussed (nor will they) on grabbing the content of another (someone else's?) url and displaying it in a div.

Why don't you try to play with the library and the three functions I gave you?

If you don't have php then simple, create a simple txt document. Then make a request for that document and put the contents inside a div. I suggest that you use myGetAjaxResponse.

Just change the onLoadClick function to ask for a different file and to respond differently than I have above when the file's contents are returned. I.e


function onLoadBtnClick()
{
var tgt = byId('resultsContainer');
myGetAjaxResponse(tgt, 'notQuiteHtml.html');
}

I've done just the same thing with files containing html, that aren't actually valid html - i.e there's no <head>, <body> or <html> tags - just the content itself.

Example:
-------start of notQuiteHtml.html------
<div id='mPageHolder' style='background-color: aqua; min-height: 400px'>
<input type='button' value='Press to run javascript' önclick='onJobsPageBtn1()'/>
</div>
-----end of notQuiteHtml.html--------
using jQuery you can easily put html in to div.

for example

JavaScript
<div id="myDiv"></div>


$(document).ready(function()
{
var htmlstr="<table><tr><td></td></tr><table>";
$('#myDiv').html(htmlstr);
});</table></table>

Please make sure that you should download the latest jQuery plugin from here http://jquery.com/download/[^]and put it in to the page
 
Share this answer
 
Comments
Gobsek 1-Aug-13 4:22am    
Thanks, But is it possible don't use JQuery totally or any other plugins ?
Jameel VM 1-Aug-13 5:29am    
use javascript
Gobsek 1-Aug-13 9:53am    
HTML source (into div tag) must be taken from external URL,

for ex. http://site.com/data.txt, but not internal value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900