multipule actions if statement in javascript - javascript

I want to create a multipule action if statement, for example this is my script now:
if (document.getElementById("username").value==null || document.getElementById("username").value=="")
{
alert("username must be filled out");
return false;
}
and I want that in the same time as the alert pops up to add another action like change to path to some image or something llike that..
anyone know how to do this?
thanks,
Lian

The alert popup in browsers is a kind of modal form.
From the moment you call the alert, user interaction with the browser is limited to the popup buttons and you cannot do anything else.
You can't offer the user "multiple actions" with an alert popup.

Try creating your own modal box as your message box.
because once you pop up an alert in js the code below will not executed unless you hit the ok button

Related

How to have control over backbutton hit in javascript?

I have used the concept from below link..It worked.
detecting browser back button in javascript/jquery without using any external plugins
but my problem is,
I have a dialog box which shows different input fields.
So, what i have did is when i click on back button i making that dialog to hide. But at the same time i am using the areyousure.js warning message at popup.. When i change any input field on popup and click on backbutton,I am getting a warning message as "You have unsaved changes. Stay on this page" or "leave this page." but when i click on the stay on this page the popup is getting hide as according to concept of that link.
So , what iam trying is, when i click backbutton after changing anyfield in dialog box , i must get that warining msg and if i click stay on page it should be stay there..
When i dont change any thing just have clicked back button i must just hide the dialog box and load the page..
Hope you all understand my problem.If so, please give me a solution.
My approach for hiding the dialog box when no changes are done on the pop up
bajb_backdetect.OnBack = function()
{
var popDiv = $('#popupModal #progressDiv');
var popIframe = popDiv.find('#pop-iframe');
popIframe.hide();
}
I cant do changes in my areyousure.js pages as it is used in most of the code. Some code dont have the dialogboxes..
So, in the current page , i need to valdate both the coditions..
I think this is the solution for you: Detect/Warn when back button is pressed
Let us know if it helps

How to clear the text of asp:TextBox when user again open the pop up window?

I follow How to clear a textbox using javascript but my scenario is little change. I have a login button and when user click on login a pop-up window is open. He fill the textbox and for some reason he go back to main page. When he again come to login popup window he previous value is not cleared. May be this is because the page is not loaded. And I do not want to load page again. I want to use JQuery/JavaScript to remove the entered text. I has idea that I write code inside Close button but I don't know how to clear the textboxes. Please help.
Here is the code, scenario is that I used popup for login and sign up. In login popup there is link "Don't have login Id" when user click on it the sign up pop up with form is open.
Don't have Login Id
And the JavaScript method is
<script type="text/javascript">
function function_deletePreviousData(){
document.getElementById("signUp").value = "";
}
</script>
But nothing happened. I think this is because I don't give element id, I just give id of element which I used to land on sign up pop up form. Any idea for clear all form without accessing all elements and give it null value.
Instead of including code in close button click event, we should write code in login button click.This is one of my suggestion.
Try this once:
Javascript:
<script type="text/javascript">
function LoginButtonClick() {
document.getElementById`("TextBox_Id").value = "";
}
</script>
JQuery:
jQuery("#LoginButton_Id").Click( function()
{
$('#TextBox_Id').val("");
} );
Finally I find the method in JQuery to reset all fields. Just Trigger reset.
$('#form').trigger("reset");
Thanks all for help.

window.onbeforeunload returns user to same page

On leaving a page with a form, I want to save the changes made without any user confirmation.
Having found this question I adapted one of the answers as follows. Note that I am not using return:
function setConfirmUnload(on) {
window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
alert('Gonna save now');
setTimeout(validate_submit,500);
}
This is only triggered when the user fills some value in the form. If an element value changes, I use
setConfirmUnload(true);
Result in FF23: when user clicks somewhere to leave the page, the alert is shown, validate_submit() is executed on "OK", new page appears - BUT now the alert reappears and the user is returned to the original page on "OK". Why? Can somebody confirm this behaviour?
The window.onbeforeunload method causes a confirm box to appear containing whatever message is returned by the function assigned to it. You can put alerts and other stuff in this method, but it will always end with a confirm message. If your function returns nothing, it will still give a message (determined by browser) saying "are you sure you want to leave this page" or something along those lines.

Javascript: Detect when an alert box is OK'ed and/or closed

How can I detect when a javascript alert box is OK'ed and/or closed?
Since alert is blocking:
alert('foo');
function_to_call_when_oked_or_closed();
Just put the function after the call to alert.
You can show a confirm box that displays a message with an OK and a Cancel button and check which button the user clicked by:
<script type="text/javascript">
var answer = confirm ("Is this working for you?")
if (answer)
alert ("Woo Hoo! Then my answer was correct.")
else
alert ("Darn. Well, keep trying then.")
</script>
If you want to make use of a simple alert box you can have a look at 1001 tutorials online such as this one for example. But your question does not specify how exactly you want to implement your alert.
http://www.tizag.com/javascriptT/javascriptalert.php
If user didn't press ok or close, the JS will not proceed to the next line. So actually there is no need to detect this.
Maybe you want to know if the user pressed ok or close. For the alert popup box, there is no easy way to tell it is ok'ed or closed. In Firefox, you don't even have the close button.
So if you really want to do this, you can use Confirm Box or Prompt Box.
Please check the link below for how to use Confirm Box or Prompt Box:
http://www.w3schools.com/js/js_popup.asp

How to block alert box through jQuery

I want to block alert box if it is present in code. Im using an api that tells me the search result of my website and if any user enter
<script>alert('Just teasing')</script>
then it shows an alert box on my page how can i stop this alert?
First of all you should sanitize you input as #Nikita commented.
If you want to accept JavaScript and only disable alert you can replace the window.alert function.
window.alert = function() { /* do nothing here */ }
Now calling alert won't do anything.
When presenting the search results back to the user you need to ensure you HTML encode the output so the user would see the script rather than it being executed.

Categories

Resources