473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JavaScript function that waits for user input before returning

Frinavale
9,735 Expert Mod 8TB
Call me crazy because I'm basically trying to re-invent the wheel here but I learn from doing things the hard way....and the sales guys want the site to look consistant.

I'm attempting to create like the JavaScript Confirm....

I already have a control that asks the user to confirm what they are doing.

It's a little complicated because there's a bunch of different aspects to the control. I guess the important part is that it was originally designed to be displayed and ask confirmation before continuing. The response that the user selects is then posted back to the web server (or may just close client side if configured to prevent postback).

Anyways, I'd like to modify this control so that it displays, waits for user input, and returns the value the user chose via JavaScript (without posting back to the server).

I have been attempting to modify the JavaScript function used to display the control so that it waits for user input and returns the value that the user selected.

The problem is that I'm not sure how to "wait for user input".

Any information, or even keywords to use in a google search, on this topic would be greatly appreciated.

Thanks again,

-Frinny
Feb 18 '09 #1
28 14994
Dormilich
8,658 Expert Mod 8TB
hm, recently (yesterday or so) someone posted here a link to a "custom alert function" (i.e. overwriting the window.alert method) maybe you're interested?

on the other hand, "waiting for input" seems to me like just another event handler (onclick - button, onchange/onselect - dropdown, etc.)
Feb 18 '09 #2
Frinavale
9,735 Expert Mod 8TB
I'll look for that other thread...

Basically, yeah it's another event....sort of?

Right now I'm applying the onclick event for a button to call the JavaScript that displays the "confirm" control. The button would never submit to the server, the "confirm" control would.

Now I'd like to be able to create a function that returns the user's selection.
So instead of just calling the JavaScript function in the onclick event and not submitting, I'd like to be able to submit according to the user's selection:
Expand|Select|Wrap|Line Numbers
  1. <input type="mybutton" onclick="return myFunctionThatWaitsForUserInput();" value="some button" />
Hopefully the other thread can help me get an idea of how to do this...I'm not great with JavaScript (just know the basics to get me by)...events are something that I have very basic knowledge of.

Thanks

-Frinny
Feb 18 '09 #3
Dormilich
8,658 Expert Mod 8TB
you're not done with just one event. you need one to bring up the confirm dialogue and another who submits the input from the confirm, doing with it what you like. a return doesn't seem sensible to me, maybe just feeding the next processing function is better? you could also store the user input in an object and access that later.

maybe I can convince you to use EventListeners, too...

PS: what is <input type="mybutton"> supposed to be for a type?
Feb 18 '09 #4
Frinavale
9,735 Expert Mod 8TB
Hehe, it's supposed to be a Button with an ID = "myButton"
You got my point though right?

I'm actually not at all interested in using the existing JavaScript confirm.

Looking back at what I posted yesterday I realise that I have not been overly clear on what I mean by "custom confirm control".

My custom confirm control is made up of a bunch of <div>s and buttons and labels and stuff. It's draggable and has a close button in the top corner. It let's me create messages on the server which I can then display in the client's browser.

I don't know the term for the techniques I'm using (and maybe there's no term at all). I needed a way to be able to have the server communicate with the user without printing values into the "real estate" area (the main content) of the page. Instead it's printed into a...um let's call it a <div> that has an CSS absolute position (so that it floats on top of the "real estate" area). My messages are displayed within that <div> and the user is able to select "yes", "no", or "cancel/other" or even "close".

It's pretty cool and I'd love to show it off (it's got rounded corners and a title bar and everything) but I can't.

So, whenever I was talking about using a "confirm" control I was referring to my custom control made of a bunch of HTML. I guess using the double quotes around the word confirm didn't help with my clarification at all....

Anyway, I'm going to do some research into event listeners to see if they can help me with my function that "waits for user input".

Thanks for your help

-Frinny
Feb 19 '09 #5
Dormilich
8,658 Expert Mod 8TB
@Frinavale
sounds a bit like AJAX, though the communication there is reversed, the client requests the server (but you can use that by regularly requesting that return an answer if something new is on the server, like a news ticker for instance)

I really like to see it (even though it's not perfect) - looks like an interesting project anyways.

@Frinavale
MDC - Event Listener
Feb 19 '09 #6
Frinavale
9,735 Expert Mod 8TB
@Dormilich
The control can be used in conjunction with Ajax calls to the server.
This is one of the cool things about it. When an asynchronous call to the server happens and validation occurs or some sort of confirmation is required, this control can be used by the server to display a response to the user... it is also used to accept input from the user to relay that back to the server.


@Dormilich
Oh it is perfect just the way it is ;)
It just needs this modification to be "more prefect".

@Dormilich
This is but a very tiny but important part of the project....It's been released along with my web application for some time now.

Now it's time for it to grow as the new features require.
Feb 19 '09 #7
Frinavale
9,735 Expert Mod 8TB
I can't see how EventListeners are going to help me.

Is there a way to Wait for an event to occur in JavaScript?
I'm not fond of the idea of looping until something occurs...I can see this causing problems and it's just ugly.
Feb 19 '09 #8
Dormilich
8,658 Expert Mod 8TB
@Frinavale
EDIT: you don't need to do anything else than registering your function, everything else is done by the system.

let's have a look at an example:

you have somewhere an element
Expand|Select|Wrap|Line Numbers
  1. <a id="example">link</a>
to this element you can attach Event Listeners (say, on page load)
Expand|Select|Wrap|Line Numbers
  1. var ref = document.getElementById("example");
  2. ref.addEventListener("click", myFuncOnClick, false); (1)
  3. ref.addEventListener("mousover", myFuncOnMouseOver, false); (2)
event (1) fires myFuncOnClick() when the user clicks on this link (no matter when)
event (2) fires myFuncOnMouseOver() when the mouse is moved over the link (no matter how often)

so speaking boldly, any registered event waits to occur without having you to worry about it. just tell the handler what function to call when the event happens.
Feb 19 '09 #9
Frinavale
9,735 Expert Mod 8TB
So...

A button is clicked and my function is called because the EventListener catches the onclick event. The button's onclick event is handled and that's that. ([edit] apparently that's Not That [/edit])

Now another button is clicked in my custom confirm control.
Another event is fired....which will be caught by another EventListener.

Now I have to either have to somehow raise the first button click event, or somehow submit the page to the server and indicate that it originated from the original button click instead of the second button click.
Feb 19 '09 #10
Dormilich
8,658 Expert Mod 8TB
1. right

2. right

3. hää? (engl.: ???) it depends on what the first two handlers trigger... you can arrange matters so that event 1 sets a special property which event 2 checks before submitting....

maybe it's time to use some of your code, because it's getting complicated for me to stay on the right track. explaining with actual code is better because Javascript offers oh so many ways to do such a task.
Feb 19 '09 #11
Frinavale
9,735 Expert Mod 8TB
@Dormilich
Could you elaborate on this?

@Dormilich
In the following I am not using my custom control.
I'm using a regular JavaScript confirm:
Expand|Select|Wrap|Line Numbers
  1. function CheckBeforeProcessing(){
  2.     if(Check('someControlsID')==true)
  3.     {
  4.         if(confirm('The check suggests that I should prompt you before continuing. Do you want to proceed?'))
  5.         {
  6.             SomeMethodThatDoesStuff();
  7.             return true;
  8.         }
  9.         else
  10.         {
  11.             return false;
  12.         }
  13.     } 
  14.     else 
  15.     {
  16.         SomeOtherMethodThatDoesStuff();
  17.         return true;
  18.     }
  19. }


Now instead of using the:
Expand|Select|Wrap|Line Numbers
  1. if (confirm('... Do you want to proceed?'))
I want to call my own function. It would display the custom confirm control and wait for the user's input. It would then return true or false based on the user's input. Like the confirm control...it waits for user input before returning to the function.

So far, the function displays the custom confirm control...but I still lack the knowledge of how to use JavaScript events to proceed.
Feb 19 '09 #12
Dormilich
8,658 Expert Mod 8TB
ok, that's something I can imagine.

Expand|Select|Wrap|Line Numbers
  1. function CheckBeforeProcessing()
  2. {
  3.     if (Check('someControlsID') == true)
  4.     {
  5.         // this brings up your confirm control
  6.         // and adds the event listeners
  7.         showConfirm();
  8.  
  9.         // a global object, where the confirm status is saved
  10.         return Frinny.confirmed;
  11.     } 
  12.     else 
  13.     {
  14.         SomeOtherMethodThatDoesStuff();
  15.         return true;
  16.     }
  17. }
let's assume you have 2 buttons (for the sake of simplicity) in your confirm dialogue:
Expand|Select|Wrap|Line Numbers
  1. <input type="button" id="yes" value="yes">
  2. <input type="button" id="no" value="no">
showConfirm has to do 2 things:
  1. bring up your control
  2. register the necessary event handlers
Expand|Select|Wrap|Line Numbers
  1. function showConfirm()
  2. {
  3.     /* your code for the confirm dialogue */
  4.  
  5.     /* code for event handling */
  6. // get the confirm button
  7.     var btn_yes = document.getElementById("yes");
  8.     var btn_no  = document.getElementById("no");
  9. // register events
  10.     btn_yes.addEventListener("click", SomeMethodThatDoesStuff, false);
  11.     btn_yes.addEventListener("click", closeConfirmDialogue, false);
  12.     btn_no.addEventListener("click", SomeMethodThatDoesStuffIfUserDenies, false);
  13.     btn_no.addEventListener("click", closeConfirmDialogue, false);
  14. }
  15.  
  16. // the object holding the confirm status
  17. // defaults to false
  18. Frinny = { confirmed: false };
  19.  
  20. // somewhere in SomeMethodThatDoesStuff()
  21. {
  22.     /* code */
  23.     Frinny.confirmed = true;
  24.     /* more code */
  25. }
Feb 19 '09 #13
Frinavale
9,735 Expert Mod 8TB
I swear I tried something similar before...although I didn't use an Object to store my value, I simply used a hidden field.

But the problem was that (see comment):
Expand|Select|Wrap|Line Numbers
  1. function CheckBeforeProcessing()
  2. {
  3.     if (Check('someControlsID') == true)
  4.     {
  5.         // this brings up your confirm control
  6.         // and adds the event listeners
  7.         showConfirm();
  8.  
  9.         // a global object, where the confirm status is saved
  10. //-------------------------------------------------------------------------------------
  11. // This was called before the button was clicked
  12.         return Frinny.confirmed;
  13. //-------------------------------------------------------------------------------------
  14.     } 
  15.     else 
  16.     {
  17.         SomeOtherMethodThatDoesStuff();
  18.         return true;
  19.     }
  20. }
Feb 19 '09 #14
Frinavale
9,735 Expert Mod 8TB
@Dormilich
This "MethodThatDoesStuff" is not linked with my custom confirm control.
It's used by other controls too...

I can't modify this to be dependent on my confirm control.
That's why I was using the CheckBeforeProcessing() method.
The CheckBeforeProcessing() method is called during a button click event:
Expand|Select|Wrap|Line Numbers
  1. <input id="someButton" type="submit" style="width: 100px;" onclick="return CheckBeforeProcessing();" value="Do Something" name="someButton"/>
I just want to prevent posting back to the server if the user does not confirm.....
(well...and execute SomeOtherMethodThatDoesStuff() if the do not confirm)
Feb 19 '09 #15
Dormilich
8,658 Expert Mod 8TB
@Frinavale
well, I noticed that too, but at that time I had trouble connecting to bytes.

I think the best is to drop the return and let the event take over.
Expand|Select|Wrap|Line Numbers
  1. //instead of
  2. if (checkBeforeProcessing())
  3. {
  4.     executeCommand();
  5. }
  6. // do
  7. button_yes.addEventListener("click", executeCommand, false);
Feb 19 '09 #16
Frinavale
9,735 Expert Mod 8TB
Urg, that's what I already have.

Fine, I'll place Multiple custom confirms on the page for each control that "might" require a confirmation. Do away with All the page's button click events and just depend on the custom confirm events instead.

I'm not happy with this though because it's going to use a lot of resources.

My custom control is not small...it has a lot of stuff attached to it.

I was really hoping to have just 1 custom confirm control on the page which would just act like a JavaScript confirm.
Feb 19 '09 #17
Frinavale
9,735 Expert Mod 8TB
:( It's going to be way too big.

There are too many controls that would require the same confirmation before continuing.

There has to be a better way to do this!
Feb 19 '09 #18
Dormilich
8,658 Expert Mod 8TB
@Frinavale
that doesn't matter. just make a function for that and add it to the event stack.
Expand|Select|Wrap|Line Numbers
  1. var setConfirm = function() 
  2. {
  3.     Frinny.confirmed = true
  4. }
  5. btn_yes.addEventListener("click", setConfirm, false);

@Frinavale
then its probably easier to submit the form only via Javascript.
like
Expand|Select|Wrap|Line Numbers
  1. var chainExec = function()
  2. {
  3.     // definition of form
  4.     form.submit();
  5. }
  6. btn_yes.addEventListener("click", chainExec, false);
  7.  
  8. // prevent form submission without confirming first
  9. <input id="someButton" type="submit" style="width: 100px;" onclick="return false" value="Do Something" name="someButton"/>
Feb 19 '09 #19
Frinavale
9,735 Expert Mod 8TB
@Dormilich
The event stack??
I'm still in the midst of reading up on JavaScript Events


@Dormilich
I thought about this too, but there are two problems with this:
1) I'm developing an ASP.NET application and other junk is added to validate that the events that happen in the page originate from a control on the page. It is unlikely that the page will validate and the user will be directed to a generic "an error occurred" page.

2) If/when the page is submitted back to the server I wont be able to tell what originally caused the post back to occur....
Feb 19 '09 #20
Dormilich
8,658 Expert Mod 8TB
@Frinavale
well, if the confirmation dialogue is only called before form submission, all you have to do is tell the confirm which form it should submit (you could use "this" for that or again the global Frinny object which knows the form to submit)
Expand|Select|Wrap|Line Numbers
  1. <form action="..." id="ex1">
  2. // form content
  3.     <input ... onclick="confirm(); return false;"> // in this case the child of <form>
  4. </form>
  5.  
  6. function confirm()
  7. {
  8.     // set the form id
  9.     Frinny.formID = this.parentNode.id;
  10.     showConfirm();
  11. }
  12. function submitForm()
  13. {
  14.     document.getElementbyId(Frinny.formID).submit();
  15. }
  16. // in showConfirm()
  17. btn_yes.addEventListener("click", submitForm, false);
Feb 19 '09 #21
Dormilich
8,658 Expert Mod 8TB
@Frinavale
I don't know if that's the right name, but you can register more than one function for an event (that's why it was invented)

@Frinavale
1) I didn't know about that. you just need to add that other junk like before (however that is done...)

2) that's probably directly depending on 1)

is there a difference if you do form.submit() or a regular submit? my Javascript resource says it's the same.
Feb 19 '09 #22
Frinavale
9,735 Expert Mod 8TB
>>HUGS<<
:D :) :) :D

Thanks Dormilich I think I know what I'm doing now!
Sorry about my slow learning...but I think I get it.

I'm going to create an JavaScript Object that represents my confirm control.
It's going to have a Confirm method.

This method will take 2 parameters: the control that is calling the confirm method, and the event that triggered the control to call the confirm method.

The confirm method will display the custom confirm control, listen for the yes or no (etc) button click events, and then return False to prevent a postback from occurring.

If the user confirms (clicks "yes") then the Object will set a property indicating so and trigger the event for the control that originally called the confirm method.

The control that originally called the confirm method will issue the confirm method again but since the user has already confirmed the method will not display the confirm control and will simply return true.

Now I just have to figure out how to implement this :)
Feb 19 '09 #23
Dormilich
8,658 Expert Mod 8TB
I think your app is too complex for me to understand so easily.... well, I cross the fingers for you.

anyways, your plan does make sense (inbetween I had something alike in mind).

did you know that everything in Javascript is an object? (even strings, numbers and functions)
Feb 19 '09 #24
Frinavale
9,735 Expert Mod 8TB
@Dormilich
I know you said this already but it just took me this long to figure out how to get what you said to work with my control.

I'm going to try and implement this tomorrow....at which time I'll probably be asking for help figuring out how to trigger the event of the original control from the method.

@Dormilich
Yeah :) It's pretty cool
Did you know that you can't create your own custom events without using an existing event?
Feb 19 '09 #25
Dormilich
8,658 Expert Mod 8TB
@Frinavale
sounds sensible.... *g*
Feb 19 '09 #26
Frinavale
9,735 Expert Mod 8TB
I'm almost ready to give up on this idea.
This is going to be very hard to implement.

The JavaScript is going to have to know things about the .NET control.
The only way to do this is through to implement an ASP.NET Ajax Server Control. I can barely understand simple JavaScript and to implement this would require extensive knowledge of Ajax and JavaScript along with knowledge about how to use the ASP.NET Ajax Framework (a library of JavaScripts).

Never mind that, but everything I've learned about ASP.NET Ajax controls is that they are "Server Controls".

I'll have to re-design my entire custom confirm control to be a Server Control instead of a User Control....I don't even know if I can use other Ajax Extenders within this so I may end up losing a lot of functionality.

Suddenly this small control is turning into a massive project.
Feb 20 '09 #27
Frinavale
9,735 Expert Mod 8TB
So I never did give up on the problem....I've made some good progress in continuing with the development of this control.

I've got to the point where I need to re-issue the event that was issued on the original control.

This is what I've tried so far:
Expand|Select|Wrap|Line Numbers
  1. _raiseOriginalEvent: function(e) {
  2.         var originalEvent = this.get_currentAssociatedControlEvent();
  3. //        originalEvent.rawEvent.initMouseEvent(originalEvent.type, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  4.  
  5.         var originalControl = this.get_currentAssociatedControl();
  6.         originalControl.dispatchEvent(originalEvent);
  7.     },
However I keep getting the following exception:


[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMEventTarget.dispatchEvent]"
nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "
JS frame :: url....] ::
anonymous :: line 146" data: no]
[Break on this error] originalControl.dispatchEvent(originalEvent);
Mar 4 '09 #28
Frinavale
9,735 Expert Mod 8TB
I solved the problem by creating a new event instead of using the original one:
Expand|Select|Wrap|Line Numbers
  1. _raiseOriginalEvent: function() {
  2.         var reissuedOriginalEvent = document.createEvent('MouseEvents');
  3.         reissuedOriginalEvent.initMouseEvent(originalEvent.type, true, true, document.defaultView,
  4.                                              1, 0, 0, 0, 0, false, false, true, false, 0, null);
  5.         var originalControl = originalEvent.target;
  6.         originalControl.dispatchEvent(reissuedOriginalEvent);
  7. }
I'm looking for a way to do this using the ASP.NET Ajax Libraries ... I haven't found one yet.

But right now, this JavaScript solution is working!

My custom confirm box displays when required and if the user clicks yes the original event is re-issued and allowed to continue. This new solution is going to save me a lot of resources both server and client side; it's cleaned up my .NET code tremendously because it attaches to multiple controls instead of a single control attaching itself to it; and the JavaScript aspect of it so cool! It still needs a lot of tweaking and testing but the hard part's done.

I've learned so much about the JavaScript language taking on this mini project and, for a change, I'm actually looking forward to my next Ajax/JavaScript enabled control :)

Thanks :)
Mar 5 '09 #29

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: reneeccwest | last post by:
Different user input boxes are automatically populated based on a value of the dropdown box. Can anyone help me on that?
5
by: Raffi | last post by:
Hi folks, I'm new to JavaScript and need some help. I have a form with a select field. Depending on what is selected in this field, I want to display or not display another select field. For...
6
by: Penny | last post by:
Hi all, I've built a simple search <Form> on a web page that is intended to allow the user to search a record store database. There is a drop down box where the user can choose either 'Artist'...
2
by: danielboendergaard | last post by:
Hey Im making a homepage in php. I use a html form to put data into mysql and i want to make some buttons which inserts user input values into a textarea. I have used a button like this: <input...
1
by: viswasai2k | last post by:
I have created a example.exe. And i am running this example.exe in the command prompt. when the example.exe is running i have to give a string input and coming next line another user input to...
3
by: Mohawk Mawk | last post by:
Hey guys. So heres yet another question to a problem where I would have no idea where to look for the answer for so I turn to you :) Say my sites administrator wants to write a news update or...
11
by: nitha | last post by:
hi there...i need help... just wondering how to put a function? this is the form: <form> <p>Room type:<br/> <input type="radio" id="room1" name="room" onclick="roomCost=150"...
7
by: Amit | last post by:
Dear Friends I need to write a Java Script for a string payment_code which comes populated from a text field , should contain only 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop '.',Comma ',',Plus...
1
by: vijay107 | last post by:
simply i take one user control and register it in one page now in this page i write a JavaScript function like this function sethere() { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.