Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Taking A Step Ahead With jQuery
Posted on 07/18/2014 at 09:24 am by Kevin Wentworth
Viewed 8,726 times | 0 comments
After spending a majority of my time developing with JavaScript and jQuery, I've come across many different things that proved very useful, but may not be thought of by most developers. Two things in particular stand out:
- The notorious download attribute, and
- Vertically aligning two DOM elements to their baseline
Both of these things have troubled developers across the globe, both new and experienced. However, a large scale problem sometimes has a very simple answer...
The Notorious Download Attribute
The download attribute on <a> tags has been seen many times, and used a few different ways. The download attribute allows an <a> tag to force the browser to download a file, instead of directly accessing it. This can be useful, for things such as PDF's, images, and printable forms.
One use of the download attribute, is as follows:
- <a download="Sample PDF" href="http://example.com/sample-pdf.pdf" title="Download Sample PDF">Download Sample PDF</a>
What this <a> element does, is accesses the http://example.com/sample-pdf.pdf file, then tells the browser to download sample-pdf.pdf as Sample PDF.pdf. Now, what if you only want them to download the file if there is no JavaScript being run? Well, I guess we'll have to access some of the attributes inside of the <a> element and mix it up a bit.
- jQuery(document).ready(function(){
- /*
- * .is("[attribute-selector]") works as .hasAttr would
- * .hasAttr does not exist
- */
- if($(this).is("[download]")) {
- $(this).removeAttr('download');
- // Remove the attribute forcing the browser to download
- $(this).attr('title', $(this).attr('title').replace('Download', 'View'));
- // Change element hover-title to say View instead of Download
- $(this).text( $(this).text().replace('Download', 'View') );
- }
- });
- });
EDIT!
After deciding that using .is('[selector]') is a clunky way to write your own code, I put together a very small jQuery plugin for .hasAttr("attributeName")
- (function($) {
- $.fn.hasAttr = function( selector ) {
- if($(this).is('[' + selector + ']')) {
- return true;
- } else {
- return false;
- }
- return this;
- }
- })(jQuery);
To use this, just drop it inside of a .js file you're using, OUTSIDE of a $(document).ready() function.
Vertically Aligning Two DOM Elements To Their Baseline
Throughout the course of the internet, people have tried to position elements as perfectly as they can, while still being smooth running and effective. While trying to do this, many people overlook very small positioning issues that can be fixed with a quick jQuery line.
The example given here, is a "Donate Now" button being vertically aligned to the WebPage's <h1> elements baseline. Both elements were contained in a common ancestral position: relative div.
- $("div#donate-now a").css({
- 'top':
- (
- // Height of <h1> from top of div
- $("h1").position().top +
- // true height of <h1>, now button is totally under <h1>
- $("h1").height()
- )
- // remove height of donate button from offset, and now..
- - $("div#donate-now a").height()
- // perfectly aligned to the baseline of the <h1>
- });
Good Luck, Fellow Programmers.
Tags for Taking A Step Ahead With jQuery
Comments for this Posting
No comments. Be the first to post a reply.
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!