简体   繁体   English

ASP.net MVC在登录时运行Javascript功能

[英]ASP.net MVC Run Javascript function on Login

Is there a way to fire a Javascript function as soon as the user logs in. I am using asp.net authentication. 有没有一种方法可以在用户登录后立即触发Javascript函数。我正在使用asp.net身份验证。 When the user logs in, regardless from where i want to fire off JavaScript:Chat_OpenMessengerDialog(). 当用户登录时,无论我想在哪里触发JavaScript:Chat_OpenMessengerDialog()。 This will open up my CuteSoft messenger window. 这将打开我的CuteSoft信使窗口。

So i just want to automatically open the chat messenger when the user has logged in. 所以我只想在用户登录时自动打开聊天信使。

Any ideas? 有任何想法吗?

Thanks 谢谢

您可以做的一件事是在您的母版页中有一个javascript块,它将完全执行此操作,但只有在IsAuthenticated为true时才会发送到客户端。

Since you are doing the login via a Post to the controller, the javascript will need to be fired from the response that is returned from the post. 由于您通过Post登录到控制器,因此需要从帖子返回的响应中触发javascript。

If the post returns a view, I would pass something to the view that tells it to fire the javascript. 如果帖子返回一个视图,我将向该视图传递一些内容,以告诉它触发javascript。 This can be a hidden field that the javascript looks for and then fires if found. 这可以是javascript查找的隐藏字段,然后在找到时触发。

If the controller method returns a redirect, you can have the login method set something in Temp Data. 如果控制器方法返回重定向,则可以在Temp Data中设置登录方法。 Then you can use a base controller method to check for the temp data value on every request, if present, it can set some hidden field that the javascript looks for like in the previous scenario. 然后,您可以使用基本控制器方法检查每个请求上的临时数据值,如果存在,它可以设置javascript在前一个场景中查找的一些隐藏字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM