Posts Tagged JavaScript
Onclick event is not fired on IE
I am pretty sure everybody knows that Internet Explorer has “a few” bugs… π
You didn’t? π― Alright, better to stay on your little cloud and leave this blog right away!
For the others, I will talk about the JavaScript event onclick which is not fired when the following requirements are matched:
- In a form;
- There is only ONE input text element;
- There is one button which has an
onclick
event assigned; - You press the ‘Enter’ button inside the input text element.
For a better understanding, let’s now take the following example:
<html> <head><title>Test</title><head> <body> <form> <input type="text" id="t1"/> <input type="submit" onclick="alert('onclick fired!'); return true;"/> </form> </body> </html>
As you can see, there is nothing difficult in this code.
Well, that doesn’t mean Internet Explorer can handle it… π
The bug occurs if you press the ‘Enter’ button inside the input text element using Internet Explorer. Indeed, the onclick
event is not fired and the text ‘onclick fired!’ is not display to the user! However, it works perfectly fine on Firefox and Safari.
The funny thing is this code works on Internet Explorer if you add another input text, even if it is hidden!
Why? Don’t ask me! π
Anyway, the following example works on IE:
<html> <head><title>Test</title><head> <body> <form> <input type="text" id="t1"/> <input type="text" style="display:none"/> <input type="submit" onclick="alert('onclick fired!'); return true;"/> </form> </body> </html>
Why are we assigning an onclick
event to the submit button?
It could be for a lot of reasons, but the main one is probably to validate the form before submitting the data.
By the way, RichFaces is very often using this event on the submit buttons.
So remember to add a hidden input text to your form if you want to allow users to use the ‘Return’ key. π
Be careful of SKIP_COMMENTS
Posted by smoreau in Tricks on 01 Dec 2009
In the early days, we used to hide JavaScript code from old browsers that do not support JavaScript.
The way of doing this was to put a one-line HTML-style comment without the closing characters immediately after the opening
<script>
tag and put//-->
at the end of the script.For example:
However if you are using this trick with the initialization parameter
facelets.SKIP_COMMENTS
set to true, the code between<!--
and//-->
won’t even be sent to the client!It simply means that the code above won’t open an alert window because it has been skipped during the page rendering.
Here is what the client will receive:
You have two solutions to avoid this situation:
SKIP_COMMENTS
parameter to false (the default is true). This can’t really hurt, your page will just be heavier depending on how much HTML comments you put on your page;For information, below is the code to put in your web.xml file to set the
SKIP_COMMENTS
parameter to false:Facelets, JavaScript, old browsers, SKIP_COMMENTS
No Comments