简体   繁体   中英

How to send a variable from ajax success to a template?

Is it possible reload a page without refresh (silently) ? Here is an example where page reload automatically. But you can easily see that the page is refreshing after certain time interval. I don't like this where end user is getting such experience. I want that the page reload but without refreshing. End user should not feel that page is refreshing. Is it possible using simple html? or jquery?

I don't want <META HTTP-EQUIV="refresh" CONTENT="5"> or setTimeout of jquery because it refreshes the page while reloading?

You must perform Ajax requests at set intervals. This topic is discussed here: How to fire AJAX request Periodically?

Don't reload the whole page. Just use ajax to reload the components that needs to be updated. jQuery is perfect for that.

Use a setInterval with an ajax request. I'll update in 1 min with the script.

var divid = 'yourDivsId'

function createRequestObject() 
{
  var obj;
  var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
    obj = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    obj = new XMLHttpRequest();
  }
  return obj;
}

function sendReq(req) 
{   
  http.open('get', req);
  http.onreadystatechange = handleResponse;
  http.send(null);
}

function handleResponse() 
{   
  if (http.readyState == 4)
  {
    var response = http.responseText;
    document.getElementById(divid).innerHTML=response;
  }
}


var http = createRequestObject();
setInterval('yourUrl', 30000); //or whatever time you want.

Should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM