页面加载后如何刷新浏览器
我有一个场景是在页面第一次或一次加载后刷新浏览器。 因为加载页面时数据无法正确显示,而当我刷新浏览器时它会显示。我不知道原因。
非常感谢
I have a scenario to refresh the browser after the page is loaded for first time or one time.
Because the data is not showing properly when the pages is loaded,and when i refresh the browser it is showing.I don't know the reason.
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您只想浏览器刷新一次?做这样的事情。
或 jquery
但老实说这只是一个临时解决方案。尽管您试图通过快速修复来掩盖它。我保证它会回来困扰你。编写良好且结构化的代码将持续一生,并且始终可以在未来的项目中重用。
So you only want the browser to refresh once? Do something like this.
or jquery
But in all honesty this is just a temporary solution. As much as you try to cover it up with quick fixes. I guarantee it will come back to haunt you. Well written and structured code will last a lifetime and can always be reused on future projects.
您总是必须使用一些 JavaScript。您想要的是刷新代码在页面完全加载时运行。您可以使用 HTML
onload
事件,但这样做有问题(例如,它会在加载任何图像之前触发)。如果您想的话,我建议使用 JQuery 的ready()
事件确保它在整个页面加载后触发。示例:
使其仅在首页加载时触发有点棘手。您可以向 URL 添加锚点后缀,以跟踪是否刷新了页面,然后仅在 URL 中不存在时才刷新:
或者,您可以使用查询字符串,因为这会自动更改时刷新页面:
警告:对于这些示例中的任何一个,如果您的用户在将“#”或“?r”标记添加到 URL 后为该页面添加书签,则该页面将不会当他们重新访问该页面时刷新。如果您希望它防弹,您可能必须使用 cookie。
Invariably, you're going to have to use some JavaScript. What you want is for your refresh code to run when the page is completely loaded. You could use the HTML
onload
event, but there are problems with this (e.g. it will fire before any images are loaded). I would suggest using JQuery'sready()
event, if you want to be sure it fires after the entire page is loaded.Example:
Making this only fire on the first page load is a bit more tricky. You could add an anchor suffix to the URL to keep track of whether you've refreshed the page yet or not, and then only refresh if it is not present in the URL:
Alternatively, you could use the query string, since this will automatically refresh the page when changed:
Caveat: With either of these samples, if your user bookmarks the page after the "#" or "?r" tag has been added to the URL, the page won't refresh when they revisit the page. If you want it to be bulletproof, you might have to use a cookie instead.