在asp.net mvc中从global.asax调用javascript函数
如何在asp.net mvc(C#)应用程序中从global.asax的Application_Start调用或制作javascript函数?
How to call or make the javascript function from the Application_Start of global.asax in asp.net mvc(C#) application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以记住会话或 cookies 中最后一次“调用”时间(这对于 javascript 来说更容易,但对于性能等来说更差),然后
您可以从 $(document).ready() 调用一次时间函数。
但请注意,浏览器可能需要几秒钟的时间来呈现页面,或者可能会遇到 404 或其他错误,并且页面将处于非活动状态...javascript 不是执行计划操作的可靠方法。
另一种方法是将计时器放在服务器上。像上面这样的 JavaScript 函数只会时不时地请求它,传递用户 ID 或类似的东西。这将防止计时器在页面重新加载期间重置。但你必须经常发出请求。因此,最好的解决方案是结合两种技术:
You can remember the last "invoked" time in Session or cookies (which is easier for javascript but worse for performance/etc) and then
You can call time function once from $(document).ready().
But note that it may take browser several seconds to render page, or it may bump into 404 or other errors and page will be inactive... javascript is not a reliable way to do scheduled actions.
Another way is to have your timer on server. JavaScript function like above will just ask for it from time to time, passing user ID or something like that. This will prevent timer reset during page reload. But you'll have to do request too often. So the best solution would be to combine two techniques:
“javascript 函数通过 jquery 从数据库获取要显示给用户的数据。javascript 函数将使用 setTimeout 定期执行”
这不是执行此操作的地方。
您是否考虑过使用您的母版页?
"The javascript function gets the data to be shown to the User from database through jquery. The javascript function will be executed periodically using setTimeout"
This wouldnt be the place to do it.
Have you thought about using your masterpage?
由于 JavaScript 在客户端执行,而 global.asax 在服务器端执行。你不能那样做。
Since JavaScript executes on client side and global.asax executes on server side. You cannot do that.
您如何在登录页面(母版页也可以)加载时检查应用程序级别变量,并在那里注册任何 javascript 并设置变量。
如果设置了变量,则可以跳过注册。
How about you check a Application level variable at the load of your landing page (master page would also do) and register whatever the javascript there and set the variable.
You can skip the registration if the variable is set.