Submit Form Only once - javascript

[Repeatedly I'm getting Notifications]. Actually, I just want to run the PHP script in cron job and i found two alternatives to submit form automatically and to click button automatically.
But these scripts are repeatedly doing it. I need to run only once.
One by using form id:
<script>
function submitForm() {
document.getElementById("form").submit();
}
window.onload=submitForm;
</script>
Another by using Button id:
<script>
jQuery(function(){
jQuery('#submit').click();
return false;
});
</script>
Can Anyone Help Me...

use sessionStorage.
<script>
function submitForm() {
var IsSubmit = sessionStorage.getItem("IsSubmit") || "N";
if(IsSubmit === "Y")
{
document.getElementById("form").submit();
sessionStorage.setItem("IsSubmit","Y")
}
}
window.onload=submitForm;
</script>

Related

How do I remember if a button has been clicked already, and remember even after page reloads using js and html?

<script type="text/javascript">
function disableButton(btn){
document.getElementById(btn.id).disabled = true;
alert("Button has been disabled.");
}
</script>
<button id="btn1" onclick="disableButton(this)">$200</button>
I'm trying to remember if a button has been clicked and still be disabled, even if the user reloads the page. I'm fine using JS and jQuery, but I'd like to not use PHP. Is there a way I could use cookies and local storage to remember it?
You can use Local Storage to achieve this.
<script type="text/javascript">
window.onload = function () {
var isButtonDisabled = localStorage.getItem('buttonDisabled') === 'true';
if (isButtonDisabled) {
document.getElementById('btn1').disabled = true;
}
};
function disableButton(btn) {
document.getElementById(btn.id).disabled = true;
alert('Button has been disabled.');
localStorage.setItem('buttonDisabled', 'true');
}
</script>

How to get the web page to redirect?

The thing is that is not working is that the page does not redirect to another page, weather if it is wrong or right. I want the page to redirect to another page if the user guesses the generated word right.
you just need to preventDefault the form submission,
use this code to prevent it
document.querySelector('form').addEventListener('submit', (e) => {
e.preventDefault();
})
function myFunction() {
if (document.getElementById("Enter_the_word").value == "KEYBOARD") {
location.href = 'index4.html';
} else {
location.href = "index.html";
}
}
Your code has no issues but please try this reversed condition. If it helps you.
MY EDITED CODE BELOW -----------------
var answerArr = ['KEYBOARD','MATH','FOOTBALL'];
if(answerArr.includes('MATH')){
location.href = 'index4.html';
}else{
location.href = 'index4.html';
}
Like this

call javascript function only once inside if statment

In this if statement I can call a function to alert a message if the statement are true but the problem I get the message more that one time every time I click on ok button on the message box I get the same message box, again and again, I want to get the message only one time (I want to call the function only one time in the if statement ) are there any way for that ?
My if statement loop
if(brd_side == COLOURS.WHITE) {
firstmassege();
} else {
secondmassege();
}
My functions :
function firstmassege() {
alert('the first massege');
}
function secondmassege() {
alert('the second massege');
}
This Code should simply work. Hope this would be useful. This displays the first message only once and the second message after the button is clicked for the second time as required.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to test the Function</p>
<button onclick="myFunction()">Click </button>
<p id="demo"></p>
<script>
var executed = false;
function myFunction() {
if (!executed){
firstmessage();
executed = true;
} else {
secondmessage();
}
}
function firstmessage() {
alert('the first message');
}
function secondmessage() {
alert('the second message');
}
</script>
</body>
</html>
How about using a flag to store the status. Please see the code below.
$('#btn').click(firstmessage)
var clicked = false;
function firstmessage() {
console.log('other statements...')
if(!clicked){
alert('the first massege');
}
clicked = true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn">click</button>
In your case it will be like below
var flag =false;
if (!flag){
firstmessage();
flag = true;
}
You can use one in jquery:
$('#myButton').one('click', function(){ alert('Hi!');});

onbeforeunload function having serious issue

I am using native.history.js to put a customised URL in the back button.
The script works fine. Now the issue is I want to make a page redirect when the refresh button is clicked; so i modified the script like this:
<script>
var back = 0;
$(window).bind('beforeunload', function () {
if (confirm('Want to continue?')) {
if (back == 1) {
alert('back');
//window.location.href = "<?=$$last_offer?>";
} else {
alert('refresh');
//window.location.href = "<?=$actual_link;?>";
}
} else {
// Do nothing!
}
});
window.onpageshow = function (e) {
if (e.persisted) {
location.reload();
}
};
window.onpopstate = function (event) {
if (document.location.toString().indexOf("redir=1") > 0) {
back = 1;
window.location.href = "<?=$$last_offer?>";
}
};
</script>
Issue is, the beforeunload function seems not working.
What is the problem I can't fin?.
If I am clicking the back button, the page is taking to the desired page, so it works fine.
All I want is that, somehow the page refresh must work as I anticipated.
Try use
$(window).on('beforeunload', function(){
return 'Are you sure you want to leave?';
});
Instead of bind, use on, i dont know what jquery version you use, but i will suggest "on".
Works fine here:
link
on jquery version 2.x(edge)

How to Prevent to redirect page before javascript alert click?

I am using below javascript code. I want to stay on same page till user click on alert.
After click on alert page should redirect to SomePage.aspx
<script type="text/javascript">
function myFunction() {
alert("Some text...");
window.location = 'SomePage.aspx';
}
</script>
Please let me know what I missed here.
Thanks.
Instead of alert i think you need confirm box :-
<script type="text/javascript">
function myFunction() {
var conf = Confirm("Some text...");
if(conf == true){
window.location = 'SomePage.aspx';
}
else{ }
}
</script>
Try Like this
Script
<script type="text/javascript">
window.alert = function (al, $) {
return function (msg) {
al.call(window, msg);
$(window).trigger("okbuttonclicked");
};
}(window.alert, window.jQuery);
$(window).on("okbuttonclicked", function () {
redirect();
});
function redirect() {
window.location.href = "http://www.google.com";
}
function myFunction() {
alert("Some text...");
}
</script>
Html
<input type="button" onclick="myFunction()" value="Click" />
Note :- This function is execute on every alert ok button. But you also pass some additional parameter for preventing to execute.
And i also recommended to use confirm instead of this.

Categories

Resources