I have one URL which asks me to " click here to login" then the next page loads. once the next page loads I need to pass the credential. tried below script but didn't work
document.getElementsByClassName('click_hereTo_login')[0].click();
$( document ).ready(function()
{ if document.location.href=="https://example.com/secondpage"
then {
document.getElementById('id').value="12322";
document.getElementById('password').value="4444";
document.getElementsByClassName('login')[0].click();
}})
First of all your if is not written correctly. It should be:
if (document.location.href=="https://example.com/secondpage"){
document.getElementById('id').value="12322";
document.getElementById('password').value="4444";
document.getElementsByClassName('login')[0].click();
}
The another problem here is, that your script runs from scratch on both pages. If you want to pass data from page to page without backend, you can use either query string or localstorage to store the data somewhere and then access it from another page.
let id = localStorage.getItem('id');
// get the input element
let idInput = document.getElementById('id');
// check if we are one second page
if(idInput) {
document.getElementById('id').value = id;
}
// Save id on login click
document.querySelector('.login').addEventListener('click', () => {
localStorage.setItem('id', 1234);
});