Automatically click button on page load - javascript

I'm a little stuck on creating an automatic page to press a 'log in' button which runs a javascript, submitting the form automatically isn't good enough as it simply doesn't log the user in (it's a javascript login system - as a test), instead, it will just return an error: http://prntscr.com/m1f1f6 it needs to automatically on load 'press' the log in button: http://prntscr.com/m1f1ur in order to perform the action of sending to "./main.php" which has an 'isLoggedIn' function.
<button class="btn btn btn-block" id="btnLoginMain">Log in</button>
Thanks in advance!

First, this is not a good approach to make the customers/users to logging in. You can use cookie or sessions to auto login your user. Still, if you want to click the button auto on web page loading you can use the below script.
<script>
window.onload=function(){
$("#btnLoginMain").click();
}
</script>
use this in the header file of your html file. If you want to click the button after some time of page load use the setTimeout() function.

You do not need click there. Check the source code, what happen when you click that button and replicate it in your script.

Clicking it is not needed to log in. Copy and paste your code for when you do press the button, and just make it appear when the page starts up.

Related

How to click a button when a page loads

Ive got to programmatically click a cookie accept button when a page loads. I tried a few different addons that are showed online. Those did not work...
The button has the following code:
<a id="acceptAllPrivacyOptionsAtag" class="bcGiveConsent bcpConsentButtonAtag bcpConsentOKButtonAtag" aria-label="accept all privacy options" role="button"> accept all cookies </a>
Is there a way to do this in javascript? I have to execute the code in bash shell when the page loads.
Well, there is a thing called Alert() it would be alright when page loads, not the best option tho.
Refering to this answer.
Add a script tag at the bottom portion of your html code and trigger a click event manually.
document.getElementById('acceptAllPrivacyOptionsAtag').click()

Popup displaying on refresh

I have a form, with a code to show a popup when I press a create/edit link. Now when I do a page refresh, I get the following popup
I have managed to stop the popup from appearing when Retry is pressed, by handling it on the code behind of my aspx, but when Cancel is pressed, the page blinks (I guess it renders again?) and the popup is shown.
It doesn't go back to the server. It just goes to the javascript function that displays the popup, and shows it.
It should be noted at this point that this popup is just a <div> which can be shown or hidden.The default property of this <div> is hidden.
Please help me solve this issue and also explain why this is happening. I haven't been able to find anything on the internet explaining this issue.
When submitting a form, content may be sent with either POST or GET.
Sending with GET appends values to the address defining what webpage you are on. It could look like this:
www.domain.tld/page?value1=apple&value2=banana
Sending with POST sends the value in a hidden field that the server receives.
Clicking "Retry" will load the website with the information currently held within the POST field. Clicking cancel should display the address you are heading to without the POST content.
I hope this answers your question. If not, is there any way for you to show the piece of code that handles the POST data?
The browser saves the data in the form when you submit it, and when you refresh the page, the browser attempts to send this data again. The popup is a warning from the browser that this is about to happen, which is important since the form could be on a shopping site, so resending the data would result in accidentally buying the same things multiple times.
To fix this, you can redirect to another page once the form has been submitted, or you can add code to reset the form so the data won't be sent again.
We should follow a best practice to solve this problem. Better have a look at this. When you press the cancel button, it simply load the previous page and values will be persisted.
My understanding so far is that when you press the cancel button, the values for the page is taken from the browser's cache. I cleared the cache to test this theory. The cache isn't just storing the values of the page but also the last server response received. In my case, the last server response was to show the the popup by calling my javascript function, along with the required values, which is what it did.
Now my work around to it was to make the closing button as a server command as well, so that the final response would be to hide the popup.
Please do let me know if there is something wrong in this explanation.

Stop script execution after refresh

I wrote a single program using html and javascript which contains a submit button. The problem that I have is that when I refresh my page, it's like I pressed submit button again!! But what I want is to NOT execut the script if I refresh the page. I don't know if it's clear but it's like that I want the program to get intialized if i refresh my page instead of executing a previous script !
I believe you are referring to the browser behaviour that is when you submit a form to the same page, if you refresh it will prompt you to resubmit the data again.
You can avoid this by issuing a redirect at the end of your script, to refresh the page and clear the history of the submit.
if (isset($_POST['submit'])) {
// ... your code here
header('Location: ' . $_SERVER['PHP_SELF']);
}

Capture click event before page load and trigger after DOM ready

I am working on an E-commerce site where on product listing page when the user clicks on Add Cart button
product will be added to cart and we are showing an popup box saying the product added... We are using Plugin for showing popup box and data for popup is coming from ajax and java controller. It is working fine well when the entire page loaded successfully. If the user clicks on Add Cart while the page load its showing plain ajax result instead of Popup because the Plugin using for Popup is not available during page load. We tried disabling the Add Cart button till page load but client is not happy with this approach.
Code:
<--!form for add to cart-->
<form submit="addcart()">
<input type="button">Add Cart</button>
</form>
//JS for show popup and save form data
saveform:function{
}
displayPopup:function{
}
productadd:function{
saveform();
displayPopup();
}
$(document).ready(function(){
productadd();
}
Is there any workaround to capture the click event during page load and trigger the click event automatically for particular button if user already clicked on. Please suggest me any other solution to show popup properly.
Thanks In Advance:)
Your option is not to use inline JS, which is not recommended nowadays anyway. In this case, you risk calling the addcart() function when the JS file has not been downloaded and parsed.
The solution is to use JS to bind the click event to the submit form upon DOM ready, so that the click event will only be meaningful once JS is loaded. For your HTML, remove the inline JS:
<form>
<input type="button">Add Cart</button>
</form>
Instead, listen to the submit event using JS:
$(document).ready(function(){
productadd();
$('form').submit(function() {
addcart();
});
});
Also, you might want to look at the reason why your site is loading so slowly (or at least the JS). Are you serving it as a gzipped file? Is there anyway to minimize it (only the production version)?

Logout of Github using javascript

I'm working on a chrome extension that will always have a current user who is logged in to GitHub. In order to switch users, I want to have a log out button in my extension that logs the current user out.
I've tried simply
chrome.windows.create({'url':'https://github.com/logout'})
Which takes me to the GitHub logout page but there is still a confirmation that the user would have to click on to successfully logout. I would like to be able to log the user out in the background or at a minimum, without them having to click on an additional confirmation.
Any thoughts?
You can check if one of the two techniques mentioned here works for you:
"How to automatically click a confirm box?": overriding the window.confirm function
"Auto Click for JavaScript confirm dialog": inserting dummy function showConfirm() at the top of the header of the Browser.Document which overwrites/suppress the original function showConfirm().
Even if the names of the methods differ in the Github confirmation page, the idea remains: if you can somehow override/silence the confirmation method, you can skip past that step.
Working Solution
When I called https://github.com/logout from an external page it showed a form with a submit/confirm button to logout. The form doesn't have an id or class but I managed to submit it using
document.getElementsByTagName('form')[1].submit();
Of course, this isn't ideal since it will break if github changes the layout of their logout page. Also, there is a delay between when the page is loaded and the form is submitted which isn't very user friendly. I'll update here if I find a better solution

Categories