Category Archives: JavaScript

Custom event creation in legacy internet explorer

Legacy IE only supports synthetic events of predefined types. It does however allow custom properties to be set on the event object, which allows us to simulate custom events. var ev = document.createEventObject(); ev.eventType = ‘mycustomevent’; ev.data = ‘some string, object or array can go here’; target.fireEvent(’ondataavailable’, ev);

Posted in JavaScript | Tagged , , | Leave a comment

Cross browser javascript event handler

Simple cross browser event handler function, that prevents the default action of the element (not following a link for example) function handler(ev) { var event = ev || window.Event, target = event.currentTarget || event.srcElement;   // do somthing… ev.preventDefault ? ev.preventDefault() : ev.returnValue = false; }

Posted in JavaScript | Tagged | Leave a comment

Javascript Date.format prototype method.

The following code add’s a format function to the Date objects prototype. It simulates the PHP date.format() function. Date.prototype.format = function(format) { var returnStr = ”; var replace = Date.replaceChars; for (var i = 0; i < format.length; i++) { var curChar = format.charAt(i); if (i – 1 >= 0 && format.charAt(i – 1) == [...]

Posted in JavaScript | Tagged , | Leave a comment

Javascript closures within an iteration.

An example of passing in the iteration value to a closure function using a self executing anonymous function. for (var i = 0; i < links.length; i++) { links[i].addEventListener(’click’, (function (n) { return function () { items[n].show(); // do something }; })(i)); }

Posted in JavaScript | Tagged | Leave a comment

Debugging click capture events on anchor tags with try/catch blocks

A common technique when build AJAX applications is to bind an event listener to an anchor tag, then do something interesting when the click event is fired. More often than not, you prevent the default action of the click event in your handler function. For example: document.getElementById(’foo’).addEventListener(’click’, function (e) { // do something e.preventDefault() }), [...]

Posted in JavaScript | Tagged , , | Leave a comment

Handle console calls in JavaScript for unsupported browsers

To prevent an erroneous console.log() call breaking your code in unsupported browsers, use the following: // make any failed console calls silent if(typeof console !== ‘object’) { console = { log: function() {}, alert: function() {}, warn: function() {}, info: function() {}, time: function() {}, timeEnd: function() {}, error: function() {} }; }

Posted in JavaScript | 1 Comment

jsTree with support for label toggle_node action AND leaf HREF click actions

You can enable node opening/closing on the label click using the UI plugin and binding the select_node.jstree event to your tree. A consequence of this method means that any leafs with a valid HREF attribute will no longer click through as expected. The method below demonstrates how to have a toggle action on clicking a [...]

Posted in JavaScript | Tagged , , | 2 Comments

Control which submit button the jQuery form validation plugin fires with

Consider the following form. <form> <input type="text" name="mytext" /> <button type="submit" name="next">next</button> <button type="submit" name="back">back</button> </form> In this case, we only want the jQuery validation plugin to do its thing when the ‘next’ button is clicked. If the back button is clicked we do not care about the validation. The solution is simple. Add the [...]

Posted in JavaScript | Tagged , | 2 Comments

Fixing the Internet Explorer BUTTON form submission bug

Internet Explorer 6/7 incorrectly handle a form submission with the <button/> element. Consider <button name=”action” value=”next”>Go to step 3</button> <button name=”action” value=”back”>Back to step 1</button> What should happen is the value of the button used clicked by the user should be posted with the action parameter. IE however posts the inner HTML of the button. [...]

Posted in JavaScript | Tagged , , , , | Leave a comment

Parse URL paramters into a JavaScript object

This function takes a URL with parameters and returns a JavaScript object with key/value pairs. If this function is called without a parameter it will use the current documents URL. url = window.location function getParameters(url) { var q = url ? url.search.substr(1) : window.location.search.substr(1), res = {}, tokens = [];   tokens = q.split(’&’); for [...]

Posted in JavaScript | Tagged , | Leave a comment