javascript 从浏览器控制台自动刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16707000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh a page automatically from the Browser console
提问by DextrousDave
Good Day
再会
I have never really used the browser console before, so I don't really know what it is capable of doing - What I want to achieve is the following:
我以前从未真正使用过浏览器控制台,所以我真的不知道它有什么能力 - 我想要实现的是以下内容:
How do I force/initiate an automatic refresh on a page from the browser console?That is, instead of having to press F5 everytime?
如何从浏览器控制台强制/启动页面上的自动刷新?也就是说,不必每次都按 F5?
Also, does the console take jquery? Or does it depend on whether the web page you are on uses jquery or not?
另外,控制台是否接受jquery?还是取决于您所在的网页是否使用jquery?
Thank you!
谢谢!
回答by Kenny
You can refresh a page from within the browser console by running the following command:
您可以通过运行以下命令从浏览器控制台刷新页面:
location.reload()
If the website you are on has jQuery loaded you will be able to run jQuery commands.
如果您所在的网站加载了 jQuery,您将能够运行 jQuery 命令。
If the website does not have jQuery loaded you can use the following bookmark to inject jQuery into the page:
如果网站没有加载 jQuery,您可以使用以下书签将 jQuery 注入页面:
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){(function(){var d=document.createElement("div"),c=document.getElementsByTagName("body")[0],e=false,g="";d.style.position="fixed";d.style.height="32px";d.style.width="220px";d.style.marginLeft="-110px";d.style.top="0";d.style.left="50%";d.style.padding="5px 10px";d.style.zIndex=1001;d.style.fontSize="12px";d.style.color="#222";d.style.backgroundColor="#f99";if(typeof jQuery!="undefined"){g="This page already using jQuery v"+jQuery.fn.jquery;return f()}else{if(typeof $=="function"){e=true}}function a(i,k){var h=document.createElement("script");h.src=i;var j=document.getElementsByTagName("head")[0],b=false;h.onload=h.onreadystatechange=function(){if(!b&&(!this.readyState||this.readyState=="loaded"||this.readyState=="complete")){b=true;k();h.onload=h.onreadystatechange=null;j.removeChild(h)}};j.appendChild(h)}a("http://code.jquery.com/jquery.min.js",function(){if(typeof jQuery=="undefined"){g="Sorry, but jQuery wasn't able to load"}else{g="This page is now jQuerified with v"+jQuery.fn.jquery;if(e){g+=" and noConflict(). Use $jq(), not $()."}}return f()});function f(){d.innerHTML=g;c.appendChild(d);window.setTimeout(function(){if(typeof jQuery=="undefined"){c.removeChild(d)}else{jQuery(d).fadeOut("slow",function(){jQuery(this).remove()});if(e){$jq=jQuery.noConflict()}}},2500)}})();});
回答by PSR
You can use like
你可以使用像
document.location.reload()
回答by V.Vachev
If you want to use the console just type document.location.reload()
in hte console. But if you want to make it automatically - it needs to be in your code.
如果您想使用控制台,只需输入document.location.reload()
hte 控制台。但是,如果您想自动生成它 - 它需要在您的代码中。
And yes, the console will take jquery if you have included the library in your code.
是的,如果您在代码中包含了该库,控制台将采用 jquery。
One more thing, for automatic refresh you need something like
还有一件事,对于自动刷新,您需要类似的东西
setInterval(function(){ document.location.reload() },10*60*1000);
But it not gonna work in the console, because after the first refresh this coed won't be in the console anymore. Just place it in your code.
setInterval(function(){ document.location.reload() },10*60*1000);
但它不会在控制台中工作,因为在第一次刷新后,这个 coed 将不再出现在控制台中。只需将它放在您的代码中。
回答by saeng
Use a browser tab "A" to open and control another browser tab "B". This allows you to continuously auto-reload a Web page in tab "B" that otherwise you have no control over. Your reload command from tab "A" will not be wiped out when tab "B" is reloaded.
使用浏览器选项卡“A”打开并控制另一个浏览器选项卡“B”。这允许您在选项卡“B”中连续自动重新加载网页,否则您无法控制。重新加载选项卡“B”时,不会清除选项卡“A”中的重新加载命令。
I use this to refresh a Website so that it does not time out and log me out. This approach does not require jQuery, and does not require you to have any control over the target Web page HTML code.
我用它来刷新一个网站,这样它就不会超时并注销我。这种方法不需要 jQuery,也不需要您对目标网页的 HTML 代码进行任何控制。
Open a browser new tab "A", press F12, select Console. Type in something like this:
打开浏览器新标签“A”,按F12,选择Console。输入如下内容:
win1 = window.open("https://www.example.com");
timer1 = setInterval(function(){win1.location.href="https://www.example.com"},10*60*1000);
The "win1" is to assign a Javascript variable name to the new browser tab "B", so that you can have Javascript control over it. The "timer1" is also to assign a variable name for good practice and for later control. The setInterVal function runs an anonymous function to call "win1" or tab "B" to refresh it's "location.href", every 10 minutes.
“win1”是为新的浏览器选项卡“B”分配一个Javascript变量名,以便您可以对其进行Javascript控制。“timer1”还用于为良好实践和以后的控制分配一个变量名称。setInterVal 函数运行一个匿名函数来调用“win1”或选项卡“B”来刷新它的“location.href”,每 10 分钟一次。
You'll probably have to keep both tabs open. "Pin" them for easy access. Close both tabs to stop the auto-reload. Hard refresh of tab "A" may also stop the auto reload. You might also be able to use the variable "timer1" to cancel the auto reload.
您可能必须同时打开两个选项卡。“固定”它们以便于访问。关闭两个选项卡以停止自动重新加载。选项卡“A”的硬刷新也可能会停止自动重新加载。您也可以使用变量“timer1”来取消自动重新加载。
回答by kaapa
If your webpage includes jQuery. then jQuery will be avaliable in the console:) To refresh you can type directly in console:
如果您的网页包含 jQuery。然后 jQuery 将在控制台中可用 :) 要刷新,您可以直接在控制台中键入:
window.location.reload()
回答by user3657103
The simplest way is to you use a plugin. "Super auto refresh" for example. Available for most of the browsers
最简单的方法是使用插件。例如“超级自动刷新”。适用于大多数浏览器
回答by Anna.P
Add this javascript
添加这个javascript
setTimeout(function () { location.reload(1); }, 5000);
Add this below meta tag to your page
将此以下元标记添加到您的页面
<meta http-equiv="refresh" content="5; URL=http://mywebsite/mypage.aspx">
Many Thanks ann
非常感谢安