第一页加载后一次页面刷新
我想实现一段 JavaScript 代码,其中指出: 如果页面已完全加载,则立即刷新页面,但仅刷新一次。 我陷入了“仅一次”:
window.onload = function () {window.location.reload()}
这给出了一个没有“仅一次”的循环。如果这有帮助的话,就会加载 jQuery。
I would like to implement a JavaScript code which states this:
if the page is loaded completely, refresh the page immediately, but only once.
I'm stuck at the "only once":
window.onload = function () {window.location.reload()}
this gives a loop without the "only once". jQuery is loaded if this helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
我会说使用哈希,如下所示:
I'd say use hash, like this:
当我遇到这个问题时,我搜索到这里,但大多数答案都试图修改现有的网址。这是使用 localStorage 对我有用的另一个答案。
When I meet this problem, I search to here but most of answers are trying to modify existing url. Here is another answer which works for me using localStorage.
由于 if 条件,页面只会重新加载一次。我也遇到了这个问题,当我搜索时,我找到了这个很好的解决方案。
这对我来说很好用。
Due to the if condition the page will reload only once.I faced this problem too and when I search ,I found this nice solution.
This works for me fine.
检查此链接,它包含一个 java 脚本代码,您可以使用该代码仅刷新页面一次
http://www.hotscripts.com/forums/javascript/4460-how-do-i-have-page-automatically-refesh-only-once.html
还有更多刷新页面的一种方法:
解决方案1:
要在每次打开页面时刷新一次页面,请使用:
解决方案2:
然后更改为
解决方案3: >
response.setIntHeader("Refresh", 1);
但是这个解决方案将刷新页面多次,具体取决于您指定的时间
我希望这对您有帮助
Check this Link it contains a java-script code that you can use to refresh your page only once
http://www.hotscripts.com/forums/javascript/4460-how-do-i-have-page-automatically-refesh-only-once.html
There are more than one way to refresh your page:
solution1:
To refresh a page once each time it opens use:
sollution2:
Then change your to say
solution3:
response.setIntHeader("Refresh", 1);
But this solution will refresh the page more than one time depend on the time you specifying
I hope that will help you
这很好用
this works perfectly
经过两个月的研究,终于找到了重新加载一次页面的解决方案。
它在我的客户端 JS 项目上运行良好。
我编写了一个函数,该函数仅重新加载页面一次。
1) 首先获取浏览器 domloading 时间
2) 获取当前时间戳
3) 浏览器 domloading 时间 + 10 秒
4) 如果浏览器 domloading 时间 + 10 秒比当前时间戳大,则可以通过“reloadPage();”刷新页面
但如果不超过 10 秒,则意味着页面刚刚重新加载,因此不会重复重新加载。
5)因此,如果您调用“reloadPage();” js 文件页面中某处的函数只会重新加载一次。
希望对某人有帮助
Finally, I got a solution for reloading page once after two months research.
It works fine on my clientside JS project.
I wrote a function that below reloading page only once.
1) First getting browser domloading time
2) Get current timestamp
3) Browser domloading time + 10 seconds
4) If Browser domloading time + 10 seconds bigger than current now timestamp then page is able to be refreshed via "reloadPage();"
But if it's not bigger than 10 seconds that means page is just reloaded thus It will not be reloaded repeatedly.
5) Therefore if you call "reloadPage();" function in somewhere in your js file page will only be reloaded once.
Hope that helps somebody
我把它放在我想要一次重新加载的页面的头标签中:
值“mc”可以设置为您想要的任何值,但两者都必须在两行中匹配。并且“=mobile”可以是“=anythingyouwant”,它只需要一个值来停止刷新。
i put this inside my head tags of the page i want a single reload on:
the value "mc" can be set to whatever you want, but both must match in the 2 lines. and the "=mobile" can be "=anythingyouwant" it just needs a value to stop the refresh.
像这样使用 window.localStorage...:
它对我有用。
Use window.localStorage... like this:
It works for me.
在
标记之后:
After
</body>
tag:我从这里得到了答案并对其进行了修改。这对我来说是完美的解决方案。
I got the Answer from here and modified it.This is the perfect solution for me.
您可以创建一个可验证的
once = false
,然后使用if else
重新加载页面,例如 ifonce == false
重新加载页面并 make Once true。You can make one verable
once = false
then reload your page withif else
like ifonce == false
reload page an make once true.您需要使用 GET 或 POST 信息。 GET 将是最简单的。你的 JS 会检查 URL,如果没有找到某个参数,它不仅会刷新页面,还会将用户发送到一个“不同”的 url,该 URL 是相同的 URL,但其中包含 GET 参数。
例如:
http://example.com --> 将刷新
,则不会刷新
http://example.com?refresh=no -->如果不刷新 如果我不想要混乱的 URL,那么我会在正文的开头包含一些 PHP,它会回显一个隐藏值,该值本质上说明初始页面请求中是否包含用于不刷新页面的必要 POST 参数。之后,您将包含一些 JS 来检查该值,并在必要时使用该 POST 信息刷新页面。
You'd need to use either GET or POST information. GET would be simplest. Your JS would check the URL, if a certain param wasn't found, it wouldn't just refresh the page, but rather send the user to a "different" url, which would be the same URL but with the GET parameter in it.
For example:
http://example.com -->will refresh
http://example.com?refresh=no -->won't refresh
If you don't want the messy URL, then I'd include some PHP right at the beginning of the body that echos a hidden value that essentitally says whether the necessary POST param for not refreshing the page was included in the initial page request. Right after that, you'd include some JS to check that value and refresh the page WITH that POST information if necessary.
尝试用这个
Try with this
请尝试使用下面的代码
Please try with the code below
这是 setTimeout 的另一个解决方案,并不完美,但它有效:
它需要当前 url 中的一个参数,因此只需将当前 url 想象成这样:
以下代码使页面仅重新加载一次:
接受的答案使用最少的数量的代码并且很容易理解。我刚刚为此提供了另一个解决方案。
希望这对其他人有帮助。
Here is another solution with setTimeout, not perfect, but it works:
It requires a parameter in the current url, so just image the current url looks like this:
The following code make the page reload just once:
The accepted answer uses the least amount of code and is easy to understand. I just provided another solution to this.
Hope this helps others.
React Hook 对我有用。
从 'react' 导入 { useEffect, useState };
React Hook worked for me.
import { useEffect, useState } from 'react';
除了这个之外,没有什么对我来说是完美的,-添加到我的 JavaScript 文件中-:
并在上一页中我写道:
nothing work for me perfectly except this, -added to my JavaScript file-:
and in the previous page I wrote:
用这个
use this
使用 rel="外部"
就像下面的例子
Use rel="external"
like below is the example