Web Development with jQuery®

(Elliott) #1

Filtering a Selection (^) ❘ 33
Explorer 8 and earlier using jQuery 1.9; this event object is automatically patched by jQuery so that
older versions of IE support the same standard event model that all the other browsers do. IE9 and
later have all this functionality built in and no longer need the patches.
The next line takes this and wraps it in a call to jQuery. By default, events are set up so that this
references the element the event is attached to. When an event occurs, jQuery leaves this default
behavior in place, so by default, you’re working with traditional JavaScript within the event callback
function. To work with jQuery again, you have to explicitly say that you want to work with jQuery,
and one way to do that is to simply wrap this in a call to jQuery.
var node = $(this);
If you had not wrapped this in a call to jQuery, the subsequent call to attr(), a jQuery function,
would have failed.
The next line verifi es whether the element has a target attribute or an href attribute. If no target
attribute is set, the call to attr('target') will return undefined, and likewise for the href attribute.
if (target === undefined && href !== undefined)
Next, after it is determined that there is no target attribute and there is an href attribute, the value
of the href attribute is examined to see whether a new window should be opened when the link is
clicked. This is done with a switch statement. Switching on true will cause the program to execute
the fi rst case statement where the expression placed beside the case statement evaluates true, and
that is the case if the value of href contains the following:
➤ (^)
http://, a non-secure web link to a third-party website
➤ (^) https://, a secure web link to a third-party website
Or if the link contains the .pdf document extension.
With these rules and some additional logic put in place throughout your website, it becomes possible
to fi sh out links to third-party websites and to PDF documents and to make those particular links
open in a new window. This works if all the links on your website are written as relative or absolute
links without the host name portion of the URL—for example http://www.example.com/, which is
the hostname portion of the URL. If some links might contain your own hostname, then you would
need to rewrite the logic presented here to fi lter out those links so that links within your website
won’t trigger false positives and open in a new window. You learn how to do that in the next section.
This is a simple but practical explanation of one possible way to use the Selectors API, to select all
the links on a given page. But what if you want to fi lter out some of the selected elements based on
other criteria, or what if you want to narrow a selection based on elements further down the tree?
This is discussed in the next section.


Filtering a Selection


jQuery is innovative in the way that it returns itself, by default, for every method call to it where it
makes sense. After you make a selection, that selection is returned as the context of an object that
can call upon any other jQuery method and that jQuery method can take the previous selection and

http://www.it-ebooks.info

Free download pdf