javascript - How to auto-refresh a page using ajax (and prevent an actual browser refresh)? - Stack Overflow

I am trying to append a small autofreshing javascript to a page that already has jQuery.I don't ha

I am trying to append a small autofreshing javascript to a page that already has jQuery.

I don't have control of the site, so I can't add a simple script to the HTML that would appear after each refresh.

I have to run the script in the console otherwise the page will only refresh once.

Here's what I'm trying:

var x = function(){
  // reload the page
  jQuery('html').load(window.location.href);

  // recurse every 5 seconds
  setTimeout(x, 5000);
};

// init
x();

This is generating an appendChild error. I'm guessing I'm just going about this a bit wrong.

I am trying to append a small autofreshing javascript to a page that already has jQuery.

I don't have control of the site, so I can't add a simple script to the HTML that would appear after each refresh.

I have to run the script in the console otherwise the page will only refresh once.

Here's what I'm trying:

var x = function(){
  // reload the page
  jQuery('html').load(window.location.href);

  // recurse every 5 seconds
  setTimeout(x, 5000);
};

// init
x();

This is generating an appendChild error. I'm guessing I'm just going about this a bit wrong.

Share Improve this question edited Mar 20, 2012 at 20:20 Mulan asked Mar 20, 2012 at 20:11 MulanMulan 135k35 gold badges240 silver badges275 bronze badges 4
  • You are trying to inject a page inside HTML Tag, exactly like appending child HTML. use location.reload() instead, this will reloads the page once, and if you want it recursively then try simpl JS like ` window.setTimeout('location.reload()', 3000);`. Hope this helps you...... – iDroid Commented Mar 20, 2012 at 20:21
  • Maybe I don't get the idea, but it seems to me that you need something called "injection". You can use Greasemonkey for Mozilla FF or an equivalent for Chrome. – Bakudan Commented Mar 20, 2012 at 20:23
  • no, this doesn't help because the script must be run through the console. Your solution and everyone other one posted is using reload() which wipes out any scripts I appended through the console after the first refresh :( – Mulan Commented Mar 20, 2012 at 20:24
  • @Bakudan, my response was for Chiller. I don't know of a Greasemoneky equivalent for Chrome and I don't feel like installing Firefox. – Mulan Commented Mar 20, 2012 at 20:35
Add a ment  | 

4 Answers 4

Reset to default 3

Despite the lack of belief that this is possible, this solution, while not perfect, is certainly working quite well

(function x(){
  jQuery('html').html('<iframe width="100%" height="' + $(window).height() + 'px" src="' + window.location.href + '"></iframe>');

  setTimeout(x, 5000);
})();

Try this:

function x(){
    setTimeout(function(){ location.reload(); }, 5000);
}();

Edit: You've made your question a little more clear. This should do the trick:

setInterval(function(){ location.reload(); }, 5000);

Maybe you want to replace the body with the contents of the page. It won't reload the whole page, so your script will stay right there. It could, however, conflict with existing scripts on the page.

How to do it:

var x = function () {
  $('body').load(window.location.pathname);

  setTimeout(x,5000);
}
x();

jQuery Reference

edit:

and now a more 'syntatically not so awful' approach: rewrite the whole document.

var x = function () {
  $.get(window.location.pathname, function (data) { 
    document.open(); 
    document.write(data); 
    document.close(); } 
  );

  setTimeout(x,5000);
}
x();

This works and do not reload the page under the body tag.

But I think is too much typing and worries for an injection hack :P

Try using window.location.reload(); like below for an auto refresh script,

<script>
    setTimeout(function () {
       window.location.reload();
    }, 5000);
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742301173a4418059.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信