html - How to login to a website using javascript? - Stack Overflow

I am trying to login to a website through java script. I have found the login form on the website and I

I am trying to login to a website through java script. I have found the login form on the website and I'm not sure what to do from here.

I have added value ="myemail" and value ="mypass" while inspecting the code and it has logged me in. I am confused on how I would implement a java script function to add to mu own code.

<input name="email" id="emailForSignIn" class="txt-input email float-label ctHidden" type="email" aria-required="true">

<input name="password" id="passwordForSignIn" autoplete="off" class="txt-input password float-label" type="password" aria-required="true">


I am trying to login to a website through java script. I have found the login form on the website and I'm not sure what to do from here.

I have added value ="myemail" and value ="mypass" while inspecting the code and it has logged me in. I am confused on how I would implement a java script function to add to mu own code.

<input name="email" id="emailForSignIn" class="txt-input email float-label ctHidden" type="email" aria-required="true">

<input name="password" id="passwordForSignIn" autoplete="off" class="txt-input password float-label" type="password" aria-required="true">


Share Improve this question asked Mar 29, 2019 at 21:40 EricEric 171 gold badge2 silver badges4 bronze badges 1
  • 1 there should be a form tag somewhere around this probably saying action="POST" meaning it will send an form-urlencoded POST request to the link in the form with all the the subelements ids and values as form – jonathan Heindl Commented Mar 29, 2019 at 21:45
Add a ment  | 

2 Answers 2

Reset to default 1

In order to 'login' to a website via Javascript you must provide an action to your form.

The form should be setup to redirect you to the next page.

However, user authentication is something that you should learn when you have more experience with Javascript. There are third-party services you can use, or you can work on the back-end yourself. Judging by your question I suspect you are learning the basics of JS.

You could do it by using ajax: https://api.jquery./jquery.ajax/

You don't need jQuery but it will make things a lot easier.

$.ajax({
    type: "POST",
    url: 'login.php', // script to do the actual authentication, set session etc.
    data: {
        username: 'foo', // get element value of username here
        password: 'bar', // get element value of password here
    },
    success: function(data) {
        // process result
    },
});

Technically you are not logging in via JS but you use JS to not reload the entire page.

If you want a plain JS-Login, you will need to store credentials in your JS, which is possible but not a good idea:

Example (not persistent, you will have to log in every time you reload the page):

var loggedIn = false;

function authenticate() {
  var password = document.getElementById('password').value;
  
  loggedIn = login(password);
  status();
}

function login(password) {
    var storedPassword = '123';

    return password == storedPassword;
}

function status() {
  if(loggedIn) {
    console.log('You are in :)');
  } else {
    console.log('You are not in :(');
  }
}
<input type='password' value='' id='password'>
<input type='button' onclick='authenticate()' value='login'><br/>
<small>Try 123 :D</small>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744938764a4602190.html

相关推荐

  • html - How to login to a website using javascript? - Stack Overflow

    I am trying to login to a website through java script. I have found the login form on the website and I

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信