Tag: javascript

  • 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.

  • The Defender D90 Project

    The Defender D90 Project

    I have a Defender D90 shell on a Vaterra Ascender chassie but it is starting to look a mess, partly due to me leaving it last minute to spray and build the truck just before the RRCI Scale Nations 2015 August meet up. We were still working on it late into Friday night ready for the morning.

    As you can see we managed to get the truck running and it looked good, this didn’t last very long. It turns out spraying 6 lays of under coat, spray and lacquer all within about 5 hours does not allow the paints to adhere correctly.

    As the shell is designed to fit a SCX10 the wheels didn’t align up perfectly leading to quite a bit of rubbing, I learnt a lot that weekend and have since decided it’s about time I redo the whole truck. I will be posting up regular updates on my progress which will hopefully be of interest.

  • Making Internet Explorer development easier

    Internet Explorer (IE) also known to some as Internet Exploder or the worst browser on the internet causes headaches for a lot of us developers. IE has what I would call a lack of tools or features, IE7  lacks the abilities to show images with a alpha channel which is something we take for granted these days, this is just one of many features used on modern websites which us developers have to combat against due to IE limitations.

    The main issue is that people are still using IE7 and IE8 which are very out dated, both of these versions of IE don’t support many of the new technologies meaning us developers have to be careful which normally results in hours of extra time writing code specific for each browser version so that the pages render correctly.

    Thank fully people have realized these issues and have written some JavaScript libraries we can use to try and fix / add features to IE to make development a lot easier.

    HTML5Shiv

    A great library I use often which adds HTML5 support to IE8 and below.

    http://code.google.com/p/html5shiv/

    IE7-JS

    Tries to fix various issues with IE7 so you can spend a considerable amount less making sure your website renders correctly in IE7.

    http://code.google.com/p/ie7-js/

  • 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.