user252718
user252718

Reputation: 11

enter credentials after the page is redirected to another page

I am new to JavaScript and web based programming languages. What I want to do is when someone opens a particular page the user must be redirected to another page and login details should be filled by the script . I don't know how to do that I have tried this:

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

The problem it only redirects, but does not enter the details

Upvotes: 0

Views: 887

Answers (2)

UzairJamal
UzairJamal

Reputation: 1

You do not have to fill the text box. Just get the credentials from URL GET and pass it to login function. No doubt your login page must have a login function.

//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
}

Upvotes: 0

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34234

You cannot apply events to another page.

For example, you can pass this username through GET parameter:

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

Then, in a access/login/ page you can insert this username using server-side language. For example, using PHP:

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

JavaScript can work with GET parameters as well, but it is much better idea to do this at server-side. If you don't have any server language, you can do this using 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];
}

My solution is definitely not the best one, it is provided just for example. You can do this in your, more elegant and convenient way.

Upvotes: 1

Related Questions