Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh Part of Page (div)

I have a basic html file which is attached to a java program. This java program updates the contents of part of the HTML file whenever the page is refreshed. I want to refresh only that part of the page after each interval of time. I can place the part I would like to refresh in a div, but I am not sure how to refresh only the contents of the div. Any help would be appreciated. Thank you.

like image 648
user760220 Avatar asked Jul 26 '13 16:07

user760220


People also ask

How do I refresh a specific part of a web page?

Use Ajax for this. Build a function that will fetch the current page via ajax, but not the whole page, just the div in question from the server. The data will then (again via jQuery) be put inside the same div in question and replace old content with new one. e.g.

How do I reload a div without reloading the whole page?

Use this. $('#mydiv'). load(document. URL + ' #mydiv');

How do I change the content of a page without reloading it?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.


2 Answers

Use Ajax for this.

Build a function that will fetch the current page via ajax, but not the whole page, just the div in question from the server. The data will then (again via jQuery) be put inside the same div in question and replace old content with new one.

Relevant function:

http://api.jquery.com/load/

e.g.

$('#thisdiv').load(document.URL +  ' #thisdiv'); 

Note, load automatically replaces content. Be sure to include a space before the id selector.

like image 195
user1721135 Avatar answered Oct 18 '22 17:10

user1721135


Let's assume that you have 2 divs inside of your html file.

<div id="div1">some text</div> <div id="div2">some other text</div> 

The java program itself can't update the content of the html file because the html is related to the client, meanwhile java is related to the back-end.

You can, however, communicate between the server (the back-end) and the client.

What we're talking about is AJAX, which you achieve using JavaScript, I recommend using jQuery which is a common JavaScript library.

Let's assume you want to refresh the page every constant interval, then you can use the interval function to repeat the same action every x time.

setInterval(function() {     alert("hi"); }, 30000); 

You could also do it like this:

setTimeout(foo, 30000); 

Whereea foo is a function.

Instead of the alert("hi") you can perform the AJAX request, which sends a request to the server and receives some information (for example the new text) which you can use to load into the div.

A classic AJAX looks like this:

var fetch = true; var url = 'someurl.java'; $.ajax( {     // Post the variable fetch to url.     type : 'post',     url : url,     dataType : 'json', // expected returned data format.     data :      {         'fetch' : fetch // You might want to indicate what you're requesting.     },     success : function(data)     {         // This happens AFTER the backend has returned an JSON array (or other object type)         var res1, res2;          for(var i = 0; i < data.length; i++)         {             // Parse through the JSON array which was returned.             // A proper error handling should be added here (check if             // everything went successful or not)              res1 = data[i].res1;             res2 = data[i].res2;              // Do something with the returned data             $('#div1').html(res1);         }     },     complete : function(data)     {         // do something, not critical.     } }); 

Wherea the backend is able to receive POST'ed data and is able to return a data object of information, for example (and very preferrable) JSON, there are many tutorials out there with how to do so, GSON from Google is something that I used a while back, you could take a look into it.

I'm not professional with Java POST receiving and JSON returning of that sort so I'm not going to give you an example with that but I hope this is a decent start.

like image 21
Jonast92 Avatar answered Oct 18 '22 17:10

Jonast92