I am trying to improve the loading speed of an HTML page.
Some Javascript methods are attached to some input fields with JQuery’s keyup() method. I have managed to safely remove all usage of JQuery in the page so far, but this is the last issue. If possible, I would like to completely get rid of JQuery on this page.
Is it an acceptable and safe practice to directly link my Javascript methods to my input field like this (from within HTML code):
<input type="text" id="my_id" onkeyup="do_something()" />
My concerns are the following:
1) I know that JQuery deals with many corners cases and bugs in browsers, but I am only interested in supporting recent browsers version (not IE 8,7,6 for example). I am scared of introducing a bug.
2) It is considered good practice to separate styling from HTML, but what about Javascript code? Is there any good reason to keep assigning my Javascript methods with JQuery?
Thanks.
This can be subjective. In our company we try to keep HTML clean of scripts and use a script file reference only. Within that file we bind the methods to the events for the fields we need to. Unobtrusive JavaScript keeps HTML clean. Be it through jQuery or JavaScript directly is up to you I guess. In addition the js files can be minimsed if required, which is what we do at the moment.
jQuery is cross-browser compatible. What works in FireFox will work in IE and Chrome (as far as I know). If you plan on supporting any version of IE and any other browser, depending on what the scripts do, you might need to work-around browser compatibility issues.
By no means does that mean use jQuery and you will find plenty of arguments in which people state you don’t need it.
In fact some jQuery methods have been known to be quite inefficient,
.live()was for example quite a memory hog I believe, which has now been deprecated foron()which apparently deals better with that issue.