Accéder au contenu principal

Reordering Javascript functions

If you have already injected some Javascript dynamically in a page (possibly because you cannot modify the original scripts directly), you may have realised the order in which Javascript functions are triggered is dependent on the order of declaration. Hence, if you try and bind a new "onclick" handler on a button for which another function has already been bound, the latter will be executed before yours. Here is a simple trick to reorder the order of execution. 

Say your original code looks like so:

<script>
function showAlert(message){
     alert(message);
}
</script>

[...]

<input type=text id="myButton" onClick="showAlert(this.value);" />


You can't modify the HTML part and yet you want to execute a new function of your own before the alert message of showAlert is sent, for instance the following one:

function changeMessage(newMessage){
      document.getElementById("myButton").value=newMessage;
}

The principle, very simple, is simply to unbind everything then reattach functions in the order you want. Below is how it woud look (note that make it even easier, I'm using jQuery):

<script>
function showAlert(message){
     alert(message);
}
function changeMessage(newMessage){
      document.getElementById("myButton").value=newMessage;
}

$(document).ready(function(){
      document.getElementById("myButton").onclick=null;
      $("#myButton").click(function(){
        changeMessage("test");
        showAlert(myButton.value);
      });
});
 
</script>


 [...]

<input type=text id="myButton" onClick="showAlert(this.value);" />


Commentaires

Posts les plus consultés de ce blog

HTML tables and auto-sizing cell heights : a workaround

Although this was common practice in the early days of the Web, working with HTML tables when it comes to building a webpage layout should be avoided as much as possible nowadays. CSS offers all the tools that are needed to create perfectly dynamic and flexible layouts, so you really should only rely on HTML tables to present actual data. This being said, it happens that you don't have the choice, and that this just what I've learned recently in my work. In the context of a client project, the framework I've had to use (Peoplesoft, an HRIS system) indeed generates HTML pages presenting a structure quite "2000"-ish, consisting in a complex nesting of HTML tables. Moreover, each time you add a component on the page (via the system itself, not when hardcoding it), a system TABLE is create to wrap the object. This can cause obviously a lot of problems, but in this case, my problem was quite specific. A few pages of the system use a well-defined table layout :

Lightbox effect with jQuery (3) - ready-to-use library

I've been pretty busy with work over the past days, but I managed to finalize my lightbox javascript library still. Although I can't confirm it is 100% cross-browser, it seems to be running well under IE8/9, Firefox and Chrome. I reckon it should work as well on Safari, but you never know. The actual library and a sample HTML file can be found here: https://sourceforge.net/projects/delightbox/files/ How to use ? First, include jQuery and, needless to say, the library. Second, call the function init from dlb , once the DOM has been fully created and the page is ready. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="./delightbox-latest.js"></script>         <script>         jQuery(document).ready(function(){             dlb.init();         }); </script> Delightbox allows to create a lightbox e

A binary clock in Javascript

Just a short post to share a tiny project I recently worked on: a binary clock. The script is written in pure Javascript (no jQuery for a change), and the design of the buttons was rapidly made in Photoshop, so nothing fancy...but I really wanted to create this :-) http://manuthommes.be/toolbox/binaryclock/ As usual, the code is not obfuscated so you can have a look and feel free to copy/edit.