页面重定向到另一个页面后输入凭据

时间:2015-11-27 04:21:23

标签: javascript redirect login

我是JavaScript和基于Web的编程语言的新手。我想要做的是当有人打开特定页面时,必须将用户重定向到另一个页面,并且脚本应填写登录详细信息。我不知道该怎么做我试过这个:

window.location.replace("http://www.example.com/access/login/");
window.onLoad(function(){document.getElementById('username').value="xxxx"});

问题只会重定向,但不会输入详细信息

2 个答案:

答案 0 :(得分:0)

您无法将事件应用于其他页面。

例如,您可以通过GET参数传递此用户名:

window.location.replace("http://www.example.com/access/login/?username=" + "xxxx");

然后,在access/login/页面中,您可以使用服务器端语言插入此username。例如,使用PHP:

<input type="text" id="username" name="username" value="<?php echo $_GET['username']; ?>"/>

JavaScript也可以使用GET参数,但在服务器端执行此操作更好。如果您没有任何服务器语言,可以使用JS:

执行此操作
var query = window.location.search.substr(1).split('=');

if (query.length === 0) return; // no username argument

if (query.length === 2 && query[0] === 'username') { 
  document.getElementById('username').value = query[1];
}

我的解决方案绝对不是最好的解决方案,例如提供。你可以用更优雅,更方便的方式做到这一点。

答案 1 :(得分:0)

您不必填写文本框。只需从URL GET获取凭据并将其传递给登录功能。 毫无疑问,您的登录页面必须具有登录功能。

//First check if you have got credentials in URL
if(is_set($_GET['username']))
{
 Your_Login_Function($_GET['username']), $_GET['password']))
}

//Your Page Code Here

Your_Login_Function(username, password)
{
 //function process goes here
}