Tag 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 | Also 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 | Also 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

ExtJS 3.3 error this.addEvents is not a function ‘statesave’

If you get something similar to the above error message in firebug while developing with Ext 3.3, a common cause if you have tried to create a component when omitting the new keyword. var dv = Ext.DataView({…}); // incorrect   var dv = new Ext.DataView({…}); // correct

Posted in Web Development | Also tagged | 1 Comment

Create CSS rules with JavaScript

Here is the cross browser technique I use to create CSS on the fly with script. I use this method when I want to manipulate certain page elements with CSS, but only when the user has JavaScript enabled. I usually place this code just after the opening body tag. <script type="text/javascript"> /* <![CDATA[ */   [...]

Posted in Web Development | Also tagged , | 1 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 | Also tagged , | Leave a 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 | Also 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 | Also tagged | 2 Comments

Symfony and TinyMCE rich text editor setup

Performed with Symfony v1.4.4 Install the sfFormExtraPlugin php symfony plugin:install sfFormExtraPlugin Download the required version of TinyMCE. Either the standalone version or the jQuery plugin version. Copy across the source files from your download into the web/js directory. Edit the view.yml to import your script. (jQuery plugin version shown below) javascripts: [jquery-1.4.2.min.js, tiny_mce/jquery.tinymce.js] Start using [...]

Posted in Symfony | Also tagged | 3 Comments