加载刷新页面后执行Javascript代码
我有一个 Javascript 文件,它分为两部分。代码的第一部分以刷新当前页面结束。我想尝试在页面重新加载后立即执行代码的第二部分,但我无法找到实现此目的的方法。我非常想要一种方法,
window.onload = someFunction()
除了由于 Javascript 的第一部分而重新加载页面后激活该功能之外。有没有有效的方法来做到这一点?
I have a Javascript file that is split into to two parts. The first part of the code ends by refreshing the current page. I would like to try to get the second part of the code to execute as soon as the page reloads, but I am having trouble finding a way to implement this. I pretty much want a way to do
window.onload = someFunction()
except that it activates the function after reloading the page due to the first part of the Javascript. Is there an effective way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Jquery 来做到这一点。
这是在页面加载时执行的
You could do that using Jquery.
This is executed on page loading
使用
document.onload = somefunction()
反而 。这将在 DOM 加载后立即执行。
你也可以使用 jQuery 来做同样的事情
Use
document.onload = somefunction()
Instead . This will get executed immediately after the DOM loads .
You can also use the jQuery to do the same like
我能想到的唯一方法是在刷新时添加查询字符串值,然后读取该值并对其进行操作。
您可以使用这样的代码:
然后在重新加载时,重定向到同一页面添加
?reloaded=1
否则(如果此标志已经引发)不要再次刷新页面以避免无限循环。The only way I can think of is to add query string value when refreshing, then read that value and act upon it.
You can use such code:
Then when reloading, redirect to same page adding
?reloaded=1
otherwise (if this flag is already raised) don't refresh the page again to avoid infinite loop.