javascript 登录成功后如何重定向用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21802172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect user after successful login?
提问by Dano007
Update: After implementing the below suggestion by Rob Sedgwick, it has become apparent that the redirect only works when the user manually "F5" refreshers the browser (Chrome). What I need to achieve is that this happens automatically in the code so the redirect happens without the user having to hot refresh. Thanks for help with this last part.
更新:在 Rob Sedgwick 实施以下建议后,很明显重定向仅在用户手动“F5”刷新浏览器(Chrome)时才有效。我需要实现的是,这会在代码中自动发生,因此重定向发生而无需用户进行热刷新。感谢您对最后一部分的帮助。
At the moment ManageTodosView from what I understand is the first action after the user has been logged in. It prints a list of to do items set up by the user. A working example can be found here http://parseplatform.github.io/Todo/and the code is https://github.com/ParsePlatform/Todo
目前,据我所知,ManageTodosView 是用户登录后的第一个操作。它打印用户设置的待办事项列表。可以在此处找到一个工作示例http://parseplatform.github.io/Todo/,代码为https://github.com/ParsePlatform/Todo
I'm using to code to really get user logins to work, I'm not to worries about what the output of the rest of the code is because the long term plan will be to remove it, for the time being its helpful to keep in place to show that the app functioning correctly.
我正在使用代码来真正让用户登录工作,我不担心其余代码的输出是什么,因为长期计划将是删除它,暂时保留它是有帮助的到位以表明应用程序正常运行。
I'm using this codeas a base to build a web app. At the moment, once the user is logged in, they are displayed data on the same page.
我正在使用此代码作为构建 Web 应用程序的基础。目前,一旦用户登录,他们将在同一页面上显示数据。
I want to be able to change this so that after they login, the user is redirected to a different page and the information is then displayed to them there.
我希望能够改变这一点,以便在他们登录后,用户被重定向到不同的页面,然后信息会在那里显示给他们。
The reason for this is that the index page is just a landing/login page and I want to redirect them to a more structured HTML page with menus, etc.
这样做的原因是索引页面只是一个登陆/登录页面,我想将它们重定向到一个更结构化的 HTML 页面,其中包含菜单等。
Within the JS code, do I just put in a redirect, something like:
在 JS 代码中,我是否只是输入了重定向,例如:
self.location="top.htm";
to this area of the code?
到这方面的代码?
// The main view for the app
var AppView = Parse.View.extend({
// Instead of generating a new element, bind to the existing skeleton of
// the App already present in the HTML.
el: $("#todoapp"),
initialize: function() {
this.render();
},
render: function() {
if (Parse.User.current()) {
new ManageTodosView();
} else {
new LogInView();
}
}
});
I have added the JS code to this JSFiddle
我已将 JS 代码添加到此JSFiddle
采纳答案by Rob Sedgwick
Update:
更新:
To address the issue of the page needing a manual fresh before the redirect works, insert
要解决页面在重定向工作之前需要手动刷新的问题,请插入
window.location.href="/someurl";
into the following code section within the todoe.js file and comment out the new ManageTodosView(); code.
进入 todoe.js 文件中的以下代码部分,并注释掉新的 ManageTodosView();代码。
Parse.User.logIn(username, password, {
success: function(user) {
window.location.href="user_home.html";
//new ManageTodosView();
self.undelegateEvents();
delete self;
},
回答by Prashant
Try this one also
也试试这个
window.open('url','_parent');
window.open('url','_parent');
回答by Robert Rowntree
I would suggest a more robust templatefor integrating Parse Todo samples with real apps. 'Marionette' offers lots of value in real world.
我建议使用更强大的模板来将 Parse Todo 示例与真实应用程序集成。“牵线木偶”在现实世界中提供了很多价值。
If you take the time to look over the app's structure and then look at the 'loginSuccess'function (scroll to very bottom of link), its pretty straightforward to redirect. You can either use the router as shown OR you can use the Marionette aggregated events which would look like:
如果您花时间查看应用程序的结构,然后查看“loginSuccess”功能(滚动到链接的最底部),则重定向非常简单。您可以使用如图所示的路由器,也可以使用 Marionette 聚合事件,如下所示:
vent.trigger('header:loggedIn', _user);
somewhere else in any module within the app....
vent.on('header:loggedIn', function (user) {
that.setViewNew(user);
});
...
setViewNew : function (user) {
User = user;
var viewOptionsUser = {
collection : this.roleList,
events : {
'keypress #new-todo': 'createTaskOnEnter'},
parentUser : User};
this.headerRegion.show(new HeaderView(viewOptionsUser));
this.mainRegion.show(new RoleRoleListCompositeView(viewOptionsUser));
}
回答by kevinn2065
Try something like this to redirect your user
尝试这样的事情来重定向您的用户
window.location = 'www.google.com'