开发者

Downloading LATEST version of a page with JQuery/JavaScript?

开发者 https://www.devze.com 2023-04-04 01:56 出处:网络
The answer on Auto refreshing with Javascript? seemed like exactly what I needed, but after a while I found something not working like I wanted it to :( When I made a timer to check the web page every

The answer on Auto refreshing with Javascript? seemed like exactly what I needed, but after a while I found something not working like I wanted it to :( When I made a timer to check the web page every 5 seconds, it kept returning the same string even after the page changed. I think this is happening because it's doing the equivilant of F5; re downloading the page only if the php script has been changed by me, and if not just sending my Javascript what's in the browser's cache, if that makes any sense. The problem is, the page isn't actually being re-uploaded every five seconds, the reason the page would change is because of the database content the PHP is displaying. What 开发者_开发知识库I would like is a function similar to $.get but will act more like Ctrl-F5, not using any cache, just re downloading the whole page. Sorry if this doesn't make any sense...

UPDATE: What I'm asking for is not a Javascript script that Ctrl-F5's the page, what I'm asking for is a function like $.get that downloads from the server no matter what. urgle, see, $.get only downloads from the server if the page has been edited since x time (and if it hasn't it'll return a copy of the page from the browser's cache), but I don't want it to do the x time thing, I just want it to download the page no matter the last-edited time.


I always throw useless query like this:

 $.get(url + '?v=' + Math.random(), success: function (data) {
    // stuff
 })

That math.random basically tricks the browser into not caching that request.


Use jQuery.ajax(). Pass through cache : false as one of the parameters.

For example, straight from the documentation:

$.ajax({
  url: "test.php",
  cache: false,
  success: function(){
    //whatever
  }
});

Source: http://api.jquery.com/jQuery.ajax/


The best solution I can imagine is to update/redirect to a page that redirects you back to current. I'm not sure if javascript has a ctrl+f5 function.


If using jQuery use the cache option with the .ajax call. Alternatively append a date stamp to the url to force the browser to fetch each time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号