Posts Tagged: JavaScript


22
Nov 10

Why won’t my jQuery work on WordPress?

I was having trouble with the demo from my jQuery plugin design pattern demo article, so I did a little research. I’d always had concerns with interoperability between the Prototype and jQuery toolkits, but never really fully understood why.

Fortunately, Chris Coyier has written an excellent article that touches on the subject. Basically, there is a conflict with the built-in dollar-sign operator that is used by both toolkits (having limited exposure to Prototype I was unaware of this).

Normally, with jQuery’s architecture, this isn’t an issue because the publicly exposed dollar-sign reference is encapsulated and is only accessed from within other self-contained plug-ins. But when you jQuery is called from outside a plug-in reference it will cause a conflict with Prototype.

The way around this is to assign a slightly customized handle when one instantiates their jQuery object and to do so by calling the “noConflict()” method when so doing. In Chris Coyier’s article, he uses $j, but one could use just about anything.

[code lang="js"]
var $j = jQuery.noConflict();

$j("#the-example").addHoverFade();
[/code]


17
Nov 10

Why jQuery Rocks – Reason #41

I’ve spent much of my afternoon looking at implementing an elegant solution to enable a degree of keyboard navigation for what is effectively an HTML slideshow.

I’d wired up a keypress event handler, but the tricky part actually turned out to be how to hide the current element and display the next (or preceding) one. Getting it to work on two slides was easy because you have an either/or situation; but once the model grows beyond that, it becomes necessary to keep a stateful record of the current position so it doesn’t get lost.

Now, jQuery has a ton of built in tools that will allow you to navigate DOM elements to your hearts content. But after banging on a bunch of these solutions for a couple hours, I realized that jQuery has all the functionality I needed built-in to its API.

One of the niftiest aspects of the query selector syntax is the pseudo-selector. Just like CSS implements a:hover and a:visited, jQuery implements mySelector:visible. That’s is! It’s so easy.

[code lang="js"]
$(document).keypress(function(e) {
// Left-arrow key event handler
if (e.keyCode == 37) {
$(".bio-panel:visible")
.hide()
.prev()
.show();
return false;
}
// Right-arrow key event handler
if (e.keyCode == 39) {
$(".bio-panel:visible")
.hide()
.next()
.show();
return false;
}
});
[/code]


9
Nov 10

A jQuery Plugin Design Pattern

I’ve been using jQuery quite extensively and I recently started encapsulating my code using the jQuery plug-in design pattern. I really like the way it reads and it’s already starting to make my code both safer and more reusable.

One thing I find myself wanting to do quite frequently is to wire up the hover events to add a specific animation behaviour on mouseover and remove it on mouseout. This kind of thing fits quite nicely with jQuery’s architecture and I was able to write a nice little self-contained plug-in that does exactly that.

The official jQuery site offers us some direction on this point:

To make sure that your plugin doesn’t collide with other libraries that might use the dollar sign, it’s a best practice to pass jQuery to a self executing function (closure) that maps it to the dollar sign so it can’t be overwritten by another library in the scope of its execution.

EDIT: I’m now hosting this plugin source publicly on GitHub:

git://github.com/quantumtom/Test-jQuery-Plugin.git

[code lang="js"]

(function($) {
$.fn.addHoverFade = function(options) {
// Use the default options
// if they haven't been overridden
var myOptions = $.extend({}, $.fn.addHoverFade.defaults, options);

return $(this)
// Set the initial fade setting
.fadeTo(myOptions.fadeSpeed, myOptions.opacityStart)

// Wire up your new method
.hover(
function(){
$(this).
fadeTo(myOptions.fadeSpeed, myOptions.opacityFinish)
},
function(){
$(this).
fadeTo(myOptions.fadeSpeed, myOptions.opacityStart)
});
};

// Initialize plugin defaults
$.fn.addHoverFade.defaults = {
fadeSpeed : "medium",
opacityStart : "0.5",
opacityFinish : "1.0"
};
})(jQuery);

[/code]

So, what we’re doing is chaining a new extension to the jQuery object. We do this by calling the public reference to the jQuery object – $ – and appending to its .fn() method. What we append to it is our new plugin and we do this by defining it as a function.

Once that’s done we define a property of our new plugin; we do this by assigning it the values of a dictionary object (remember, no such thing as an associative array in JavaScript).

Lastly, we pass all this back to the internal reference of the jQuery object. Ta-dah! You’ve wired up the plugin back into jQuery.

Accessing your new jQuery Plugin’s API extension

[code lang="js"]
$(".my-html-class-attribute").addHoverFade();
[/code]

Or we can specify to override the middle-of-the-road default options:

[code lang="js"]
$("#the-example").addHoverFade({ opacityStart : "0.35", opacityFinish : "0.95", fadeSpeed : "fast" });
[/code]

TL;DR

“So, where’s the demo?”

Here:

Not much to it, really. Now you have access to your custom method by just tagging it on to the end of your jQuery selector statement. You can optionally adjust the default configuration by specifying custom parameters when you call the method.


23
Apr 10

Add A Number To Another Number In JavaScript

Always use jQuery.

Downvote for not enough jQuery.


20
Apr 10

Fix for IE Choking on __flash__removeCallback

I’ve been getting a Flash issue in IE. The problem only appeared in IE (all versions) and not in any of the other browsers. I’m not sure if this is because Microsoft’s jScript engine is super-compliant or super-buggy. But we figured out what the problem was.

The issue we were having dealt with the HTML we used to instantiate the Flash file as an object in the DOM. We wrap an embed element in an object element, becuase IE likes one and most other broswers like the other.

The fix was simple. Without getting into too much detail, just make sure that your object and embed tags have unique and valid id attributes. There is some hidden JavaScript that the Flash player calls and if you don’t have these attributes IE chokes when it tries to call these “secret” JavaScript libraries.