Tag: jquery

  • Easy mobile browser detection for several major languages and web servers

    All app’s and websites these days need to work for several devices including desktop, tablets and mobiles. It’s not always simple detecting the type of device being used and it can sometimes be tedious adding support to your app for something which seems very simple.

    Does your chosen platform not have a pre-defined way of detecting a mobile browser? Check out Detect Mobile Browsers, has several copy / paste examples for several programming languages.

    http://detectmobilebrowsers.com/

    Remember Apache, IIS and nginx detection examples also present for doing for pesky redirects.

  • CSS & Javascript Minify / Compression

    Optimization is a key part of any website we need to keep it loading quickly for the sake of keeping visitors and also for lowering server stress. With internet connections becoming faster most developers skip optimization but mobile broadband and phones are growing rapidly which still have a relatively slow connection when compared to the common household internet connection.

    Even if a website has a quick loading speed why not try to shave of half a second loading speeds it might not seem allot but it is noticeable especially on a mobile connection.

    Minify (also known as compacting or compression but its not true compression) is a method used to lower the file size of text based files by removing unneeded white space, line breaks, comments and placing the code as compact together without disruption functionality. This can turn those 100KB files into 2KB files and it’s the simplest thing to do it could take your 5 minutes.

    CSS Compression

    I use the following website to compress my CSS files its simple and quick to use.

    http://www.csscompressor.com/

    You simply copy your CSS into the big text field and selection the options you want for best compression I would suggest ticking all boxes and changing compression mode to greatest. Here is a comparison of my websites main style.css file on the left we have the uncompressed version and then the compressed version.

    Its only 12,310 bytes difference but that’s now 12,310 bytes less each of my visitors need to download, 12,310 bytes less bandwidth I use each time then download this file and a speed increase especially for mobile devices.

    JavaScript Compression

    I use the following website to compress all my JavaScript files its simple and quick to use just like the css compression website.

    http://javascriptcompressor.com/

    This works very similar to the CSS compression website you simply paste your JavaScript into the top text field click compress and then your new code appears bellow. There isn’t any options but it does exactly what I need which is strip all white spaces, line breaks, comments and compact it together. Here is an example of one of my Javascript files, uncompressed on the left and compressed on the right.

    As you can see there is a 12,000 bytes difference.

    Issues

    The only issues I have found is due to programming errors such as missing “;” characters, due to all the code being compressed onto one line you must make sure that at the end of all CSS and JavaScript lines you have a “;” character which you should be doing already.

    You should always keep a copy of the uncompressed file but for those who lose this file you can always uncompress it using the following websites.

    http://www.javascriptbeautifier.com/

    http://mabblog.com/cssoptimizer/uncompress.html

  • Capitalise the first letter of a group of words quickly & easily

    Sometimes as a web developer I have to capitalise the first letter of each word normally for title and header tags but I don’t want to have to sit there manually capitalising each letter. HTML and CSS don’t have a native feature which allows you to quickly capitalise the first letter automatically so we have to look at using JavaScript to achieve this.

    This example uses jQuery a JavaScript library which extends the capabilities of JavaScript.

    It is best to place this code in the <head></head> tags after the jQuery library include link.

    The following code will capitalise the first letter of each word which is in a header tag. A header tag looks like the following h1, h2, h3, h4, h5 and h6.

    You can change the elements which are capitalised by adjusting the following line.

    If you want to capitalise the first letter of each word within a “h1” tag change it to the following.

    If you want to capitalise the first letter of each word which is within a element with a class of “title” change it to the following.

  • jQuery: Using prototype and jquery together

    JavaScript is a powerful tool for a website but to extend the capabilities and functionality extra libraries can be included, the most commonly used libraries are jQuery and Prototype. Normally a website will choose either of these libraries but sometimes both libraries are required, if this is the case a small change needs to be performed to stop the two libraries conflicting.

    Both jQuery and Prototype try to bind to the $ character for easy and quick referencing but due to this bind both libraries conflict leading to one loading incorrectly.

    Resolving the conflict

    Jquery has a noConflicts() function which when used stops it binding to the normal “$” character we can still access jQuery simply by replacing “$” with jQuery. This is how we call the noConflicts() function.

    It must be placed straight after the line of code which includes the jQuery library. After that the following code will no longer work due to jQuery not binding to “$” but this will resolve all compatibility issues between the two libraries.

    The above can still be achieved by using the following instead.

    You can read into this more from the official documentation found at:
    http://api.jquery.com/jQuery.noConflict/

    Bind jQuery to another character

    Typing jQuery every time we want to reference jQuery can be long winded so we can rebind jQuery to another variable for quick and easy referencing. The following will bind jQuery to “$j”.

    Now we can type the commands used above like so

    It has come to my attention that if call the noConflicts function and assign the output to a variable it will perform both the unbinding and new binding in one step.

    The example above will unbind from the normal “$” character and rebind to the new character which in this case is “j”.

    I would recommend that when you include the jQuery library files that you called the noConflicts() function immediately after and then perform your binding.

    Extra / Advanced

    Unbinding and rebinding is probably the easiest but it’s still possible to use the “$” character even when you have unbound jQuery using the noConflict function. JQuery offers the ability to continue using the “$” character in specific instances normally with in a function or call back, this allows us to continue using “$” for all are jQuery referencing at a specific point but it will not cause conflicts between other libraries. Below is an example of code which allow you to continue using “$”.

    By providing the “$” character in the function arguments we tell jQuery that we want to temporally bind jQuery to “$” but only for the code during this function and straight after jQuery lets go of the binding and the other libraries doesn’t even know. Here is more examples.

    Conclusion

    It may not be the solution you’re looking for but it works, I prefer jQuery so would like to rebind Prototype and leave jQuery bound to “$” but Prototype doesn’t have this ability so the only solution to using both of the libraries is to rebind jQuery.

    After writing this I found out about the ability to temporarily continue using jQuery alias “$” within certain instances, I have update this post and you can read these changes in the Extra / Advanced section. Using these methods gives you all your normal operation of jQuery if used correctly.