{ "status": "ok", "count": 7, "count_total": 7, "pages": 1, "posts": [ { "id": 115, "type": "post", "slug": "make-your-mouse-events-touch-friendly-with-jquery-tutorial", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/make-your-mouse-events-touch-friendly-with-jquery-tutorial\/28\/04\/2012\/", "status": "publish", "title": "Make Your Mouse Events Touch Friendly With jQuery (Tutorial)", "title_plain": "Make Your Mouse Events Touch Friendly With jQuery (Tutorial)", "content": "

With the ever increasing mobile and tablet domination the market, more and more users are visiting websites with touch devices. It is becoming more important to have both a mobile site and tablet optimisation of your desktop site. <\/p>\n

One of the main things we need to focus on is event handling. Take a mouse event example for instance; we have a drop-down menu with mouse over events to trigger the drop-down. A user with a tablet will touch your menu item and instead of seeing further options, they will instead be redirected to the page they touched. <\/p>\n

This can have a seriously negative impact on conversion and heavily increase bounce and exits. <\/p>\n

In this tutorial we will write a simple jQuery function that prevents a mouse events default action and provides listeners for touchstart and touchend events.<\/p>\n

The Code<\/h2>\n\n
\/\/\/\/\/\/\/\/\/<\/span>\n\/\/\/ Our Function<\/span>\n\/\/\/\/\/\/\/\/\/<\/span>\nfunction<\/span> tabletTouch(<\/span>)<\/span> {<\/span>\n \n\t\/\/\/\/\/\/\/\/\/<\/span>\n\t\/\/\/ Settings Object<\/span>\n\t\/\/\/\/\/\/\/\/\/<\/span>\n\tvar<\/span> settings =<\/span> {<\/span>\n\t\tobjHoverItem:<\/span> '.hoverItem'<\/span>,<\/span>\n\t\tobjClickItem:<\/span> '.clickItem'<\/span>\n\t}<\/span>\n \n\t\/\/\/\/\/\/\/\/\/<\/span>\n\t\/\/\/ Scan For Touch Device<\/span>\n\t\/\/\/\/\/\/\/\/\/<\/span>\n\tif<\/span> (<\/span>"ontouchstart"<\/span> in<\/span> document.documentElement<\/span>)<\/span> {<\/span>\n\t\t\/\/Set our event strings to be touch events<\/span>\n\t\tstrClick =<\/span> 'touchend'<\/span>;<\/span>\n\t\tstrOver =<\/span> 'touchstart'<\/span>;<\/span>\n\t\t\/\/A string that determines which device we are using<\/span>\n\t\tstrDevice =<\/span> 'Touch Device'<\/span>;<\/span>\n\t}<\/span>\n\telse<\/span> {<\/span>\n\t\t\/\/Set our event strings to be mouse events<\/span>\n\t\tstrClick =<\/span> 'click'<\/span>;<\/span>\n\t\tstrOver =<\/span> 'mouseover'<\/span>;<\/span>\n\t\t\/\/A string that determines which device we are using<\/span>\n\t\tstrDevice =<\/span> 'Normal Device'<\/span>;<\/span>\n\t}<\/span> \n \n\t\/\/\/\/\/\/\/\/\/<\/span>\n\t\/\/\/ Some simple test events<\/span>\n\t\/\/\/\/\/\/\/\/\/<\/span>\n \n\t\/\/Mouseover event<\/span>\n\t$(<\/span>settings.objHoverItem<\/span>)<\/span>.bind<\/span>(<\/span>strOver ,<\/span> function<\/span>(<\/span>e)<\/span>{<\/span>\n                \/\/Prevent any default actions <\/span>\n                e.preventDefault<\/span>(<\/span>)<\/span>;<\/span>\n\t\talert<\/span>(<\/span>'This is a "Hover" event with a '<\/span> +<\/span> strDevice)<\/span>;<\/span>\t\t\t\t\t\t\t\t\t\t\t\t \n\t}<\/span>)<\/span>;<\/span>\n \n\t\/\/Click event<\/span>\n\t$(<\/span>settings.objClickItem<\/span>)<\/span>.bind<\/span>(<\/span>strClick ,<\/span> function<\/span>(<\/span>)<\/span>{<\/span>\n\t\talert<\/span>(<\/span>'This is a "Click" event with a '<\/span> +<\/span> strDevice)<\/span>;<\/span>\t\t\t\t\t\t\t\t\t\t\t\t \n\t}<\/span>)<\/span>;<\/span>\n}<\/span><\/pre><\/div><\/div>\n\n

This script is pretty self explanatory. We simply run a function to test the device of the user. We then define some string events for the later bind handlers to use in order to provide the desired interaction.<\/p>\n

And that is that. Very simple and very effective. Let me know your thoughts below.<\/p>\n

Happy Coding!! <\/p>\n", "excerpt": "With the ever increasing mobile and tablet domination the market, more and more users are visiting websites with touch devices. It is becoming more important to have both a mobile site and tablet optimisation of your desktop site. One of the main things we need to focus on is event handling. Take a mouse event [...]", "date": "2012-04-28 12:11:17", "modified": "2012-04-28 22:05:03", "categories": [ { "id": 7, "slug": "javascript", "title": "JavaScript", "description": "", "parent": 4, "post_count": 5 }, { "id": 8, "slug": "jquery", "title": "jQuery", "description": "", "parent": 4, "post_count": 4 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" }, { "id": 64, "type": "post", "slug": "define-current-page-location-with-javascript-tutorial", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/define-current-page-location-with-javascript-tutorial\/22\/04\/2012\/", "status": "publish", "title": "Define Current Page Or Location With Javascript (Tutorial)", "title_plain": "Define Current Page Or Location With Javascript (Tutorial)", "content": "

This is a small and extremely powerful bit of JavaScript. I searched for far too long to find something this useful when all I wanted to do was reference a part of a url to find out where I was on a website.<\/p>\n

This is especially useful if you want to call a specific bit of functionality for a specific page and nowhere else (eg. a form validator on a contact page). It works in all browsers and for me it is a really solid solution that is really easy to implement.<\/p>\n

The Code<\/h2>\n\n
if (window.location.href.indexOf("the-page-you-want") >= 0) {\r\n     \/\/Do something funky\r\n}<\/pre><\/div><\/div>\n\n

The possibilities are endless with this sort of trick. Comments below<\/p>\n", "excerpt": "This is a small and extremely powerful bit of JavaScript. I searched for far too long to find something this useful when all I wanted to do was reference a part of a url to find out where I was on a website. This is especially useful if you want to call a specific bit [...]", "date": "2012-04-22 13:50:01", "modified": "2012-04-23 11:31:14", "categories": [ { "id": 7, "slug": "javascript", "title": "JavaScript", "description": "", "parent": 4, "post_count": 5 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" }, { "id": 62, "type": "post", "slug": "get-posts-from-category-name-wordpress-snippet", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/get-posts-from-category-name-wordpress-snippet\/21\/04\/2012\/", "status": "publish", "title": "Get Posts From Category Name WordPress (Snippet)", "title_plain": "Get Posts From Category Name WordPress (Snippet)", "content": "

A useful trick I always use when working with WordPress is calling on posts from a named category. Although pages are really good these days, but I still prefer to use WordPress for what it is strongest at, organising posts.<\/p>\n

Most of the time you are going to want a small loop to act as a feed placed on your website. The beauty of this snippet is that I can place it anywhere and it is incredibly clean and light.<\/p>\n

Basically all you need to do is provide the category name(cat_name) and how many posts you want to show and bob’s your uncle.<\/p>\n

The Code<\/h2>\n\n
<?php<\/span> query_posts(<\/span>'category_name=cat_name&showposts=5'<\/span>)<\/span>;<\/span> ?><\/span>\n     <?php<\/span> while<\/span> (<\/span>have_posts(<\/span>)<\/span>)<\/span> :<\/span> the_post(<\/span>)<\/span>;<\/span> ?><\/span>\n          <h3><?php<\/span> the_title(<\/span>)<\/span>;<\/span> ?><\/span><\/h3>\n          <p><?php<\/span> echo<\/span> substr<\/span>(<\/span>get_the_content(<\/span>)<\/span>,<\/span> 0<\/span>,<\/span>450<\/span>)<\/span>.<\/span>"[...]"<\/span>;<\/span> ?><\/span><\/p>\n          <a href="<?php<\/span> the_permalink(<\/span>)<\/span>;<\/span> ?><\/span>">Read More<\/a> \n     <?php<\/span> endwhile<\/span>;<\/span> ?><\/span><\/pre><\/div><\/div>\n\n

The eagle eyed amongst you all may have noticed my alternative content call. Its a simple string trim based on characters rather than words. I personally prefer this but you can put in whatever you like and it will still work.<\/p>\n

Happy coding! <\/p>\n", "excerpt": "A useful trick I always use when working with WordPress is calling on posts from a named category. Although pages are really good these days, but I still prefer to use WordPress for what it is strongest at, organising posts. Most of the time you are going to want a small loop to act as [...]", "date": "2012-04-21 23:49:03", "modified": "2012-04-23 11:34:52", "categories": [ { "id": 9, "slug": "php", "title": "PHP", "description": "", "parent": 4, "post_count": 2 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 }, { "id": 5, "slug": "wordpress", "title": "Wordpress", "description": "", "parent": 4, "post_count": 2 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" }, { "id": 59, "type": "post", "slug": "slide-out-tab-jquery-tutorial", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/slide-out-tab-jquery-tutorial\/21\/04\/2012\/", "status": "publish", "title": "Slide Out Tab jQuery Tutorial", "title_plain": "Slide Out Tab jQuery Tutorial", "content": "

A slide out tab is very useful for concealing content to make it optional, rather than in your face. It is very easy to achieve and can really heighten your end-users experience.<\/p>\n

There are countless plugins out there that do this already, but I think a lot of them are very bloated and unnecessary. I have written this little function in a classic sense so that you can use it verbosely or within a namespaced JavaScript<\/a> environment.<\/p>\n

The simple logic is that if the panel is out, push it back in, otherwise pull it out. We define whether or not it is in or out with a simple class that is added or removed.<\/p>\n

The Code<\/h2>\n

The HTML is very straight forward. Just two DIVS and some dummy content.<\/p>\n\n

<!-- Panel -->\r\n<div class="panel">\r\n\t<p>\r\n\t    Suspendisse potenti. Proin arcu diam, dignissim ac \r\n            luctus eget, mollis quis libero. Nullam pulvinar \r\n            convallis augue ac iaculis. Morbi interdum massa in.\r\n\t<\/p> \r\n\t<!-- Trigger -->\r\n\t<div class="trigger">A<br \/>B<br \/>O<br \/>U<br \/>T<\/div>\r\n<\/div><\/pre><\/div><\/div>\n\n

The CSS is pretty straight forward, we make a panel with a nested trigger. I have this fixed but you can also use absolute positioning if you wish (make sure you add overflow-x:hidden to your body tag if you do).<\/p>\n\n

.panel<\/span> {<\/span>\n\twidth<\/span>:<\/span>500px<\/span>;<\/span>\n\tpadding<\/span>:<\/span>40px<\/span>;<\/span>\n\tposition<\/span>:<\/span>fixed<\/span>;<\/span>\n\tleft<\/span>:<\/span>-580px<\/span>;<\/span>\n\ttop<\/span>:<\/span>29.5%<\/span>;<\/span>\n\tz-index<\/span>:<\/span>1<\/span>;<\/span>\n        background<\/span>:<\/span>#f2f2f2<\/span>;<\/span>\n}<\/span>\n\t.panel<\/span> .trigger<\/span> {<\/span>\n\t\twidth<\/span>:<\/span>50px<\/span>;<\/span>\n\t\ttext-align<\/span>:<\/span>center<\/span>;<\/span>\n\t\tcolor<\/span>:<\/span>#fff<\/span>;<\/span>\n\t\tbackground<\/span>:<\/span>#570f09<\/span>;<\/span>\n\t\tposition<\/span>:<\/span>absolute<\/span>;<\/span>\n\t\ttop<\/span>:<\/span>0<\/span>;<\/span>\n\t\tright<\/span>:<\/span>-45px<\/span>;<\/span>\n\t\tpadding<\/span>:<\/span>20px<\/span> 0<\/span> 20px<\/span> 2px<\/span>;<\/span>\n\t}<\/span><\/pre><\/div><\/div>\n\n

The jQuery is also very simple. As said before, we flag its status with a class and the rest is very straight forward. I have made a JavaScript settings object for scalability reasons.<\/p>\n\n

\/\/\/\/\/\/\/<\/span>\n\/\/\/Settings object<\/span>\n\/\/\/\/\/\/\/<\/span>\nvar<\/span> settings =<\/span> {<\/span>\n\tobjSlideTrigger:<\/span> '.trigger'<\/span>,<\/span>\n\tobjSlidePanel:<\/span> '.panel'<\/span>\n}<\/span>\n\/\/\/\/\/\/\/<\/span>\n\/\/\/Slide out tab<\/span>\n\/\/\/\/\/\/\/<\/span>\nfunction<\/span> slideOutTab(<\/span>)<\/span> {<\/span>\n\t\/\/Bind a click handler to the trigger<\/span>\n\t$(<\/span>settings.objSlideTrigger<\/span>)<\/span>.bind<\/span>(<\/span>'click'<\/span> ,<\/span> function<\/span>(<\/span>)<\/span> {<\/span>\n\t\t\/\/If the panel isn't out<\/span>\n\t\tif<\/span>(<\/span>!<\/span>$(<\/span>settings.objSlidePanel<\/span>)<\/span>.hasClass<\/span>(<\/span>'out'<\/span>)<\/span>)<\/span>{<\/span>\n\t\t\t\/\/Animate it to left 0px<\/span>\n\t\t\t$(<\/span>settings.objSlidePanel<\/span>)<\/span>.animate<\/span>(<\/span>{<\/span>\n\t\t\t\t'left'<\/span> :<\/span> '0px'<\/span>\n\t\t\t}<\/span>)<\/span>;<\/span>\n\t\t\t\/\/Add the out class<\/span>\n\t\t\t$(<\/span>settings.objSlidePanel<\/span>)<\/span>.addClass<\/span>(<\/span>'out'<\/span>)<\/span>;<\/span>\n\t\t}<\/span>\n\t\telse<\/span> {<\/span>\n\t\t\t\/\/Otherwise, animate it back in<\/span>\n\t\t\t$(<\/span>settings.objSlidePanel<\/span>)<\/span>.animate<\/span>(<\/span>{<\/span>\n\t\t\t\t'left'<\/span> :<\/span> '-580px'<\/span>\n\t\t\t}<\/span>)<\/span>;<\/span>\n\t\t\t\/\/Remove the out class<\/span>\n\t\t\t$(<\/span>settings.objSlidePanel<\/span>)<\/span>.removeClass<\/span>(<\/span>'out'<\/span>)<\/span>;<\/span>\n\t\t}<\/span>\n\t}<\/span>)<\/span>;<\/span>\n}<\/span><\/pre><\/div><\/div>\n\n

And to call the function simply fire it off in a document ready function.<\/p>\n\n

\/\/\/\/\/\/\/<\/span>\n\/\/\/Fire off the function<\/span>\n\/\/\/\/\/\/\/<\/span>\n$(<\/span>document)<\/span>.ready<\/span>(<\/span>function<\/span>(<\/span>)<\/span> {<\/span>\n\tslideOutTab(<\/span>)<\/span>;<\/span>\n}<\/span>)<\/span>;<\/span><\/pre><\/div><\/div>\n\n

And that is that. Pretty simple aye? If you want to see it working, look at the homepage<\/a> of this site.<\/p>\n

Happy coding!!<\/p>\n", "excerpt": "A slide out tab is very useful for concealing content to make it optional, rather than in your face. It is very easy to achieve and can really heighten your end-users experience. There are countless plugins out there that do this already, but I think a lot of them are very bloated and unnecessary. I [...]", "date": "2012-04-21 23:48:06", "modified": "2012-04-23 11:39:20", "categories": [ { "id": 6, "slug": "css", "title": "CSS", "description": "", "parent": 4, "post_count": 1 }, { "id": 7, "slug": "javascript", "title": "JavaScript", "description": "", "parent": 4, "post_count": 5 }, { "id": 8, "slug": "jquery", "title": "jQuery", "description": "", "parent": 4, "post_count": 4 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" }, { "id": 11, "type": "post", "slug": "solution-white-screen-of-death-wordpress", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/solution-white-screen-of-death-wordpress\/20\/04\/2012\/", "status": "publish", "title": "(SOLUTION) White Screen Of Death WordPress", "title_plain": "(SOLUTION) White Screen Of Death WordPress", "content": "

Many of you may have suffered what I suffered. When you update anything on your WordPress back-end, you get a white screen of death.<\/p>\n

The cause of this is simply and error with the core system. The reason the screen is pure white is that your debug mode is set to false. To see the errors, simple go to your wp-config.php <\/strong>file and look for WP_DEBUG<\/strong>. Set this flag to true and your errors will be visible. Make sure you turn this off when you set your site to live however, the last thing you want your users to see is a PHP error!!<\/p>\n

The solution to my white screen of death was a dodgy WordPress plugin and white-space in my functions.php <\/strong>file. The later is an easy fix, just simply make sure there is no white-space before and after your opening and closing PHP tags.<\/p>\n

The plug-in issue was solved by renaming my plugins<\/strong> folder to plugins_old<\/strong>. This makes WordPress deactivate all of your plug ins. Next install each plug-in one by one in the new plugins<\/strong> folder to filter out the dodgy one.<\/p>\n

And that’s that! Any questions, just comment below<\/p>\n

 <\/p>\n", "excerpt": "Many of you may have suffered what I suffered. When you update anything on your WordPress back-end, you get a white screen of death. The cause of this is simply and error with the core system. The reason the screen is pure white is that your debug mode is set to false. To see the [...]", "date": "2012-04-20 20:21:50", "modified": "2012-04-23 11:42:57", "categories": [ { "id": 9, "slug": "php", "title": "PHP", "description": "", "parent": 4, "post_count": 2 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 }, { "id": 5, "slug": "wordpress", "title": "Wordpress", "description": "", "parent": 4, "post_count": 2 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" }, { "id": 78, "type": "post", "slug": "namespace-javascript-template-tutorial", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/namespace-javascript-template-tutorial\/19\/04\/2012\/", "status": "publish", "title": "Namespace JavaScript Template (Tutorial)", "title_plain": "Namespace JavaScript Template (Tutorial)", "content": "

One of the most useful things I use in my job is a JavaScript namespace template. It is really useful for body-guarding variables and functions and writing well organised code.<\/p>\n

This is especially useful if you write extensive and complex programs for your website. A collection of un-organised jQuery plugins for example will result in poor performance and maintainability which are two of the worst things that a front end developer can ask for if they code in a commercial environment.<\/p>\n

So lets get started!<\/p>\n

The Code<\/h2>\n

The namespace allows you to have “public” and “private” properties. A public property is returned at the end of the namespace and the private properties can be accessed within the namespace.<\/p>\n

For the purposes of this tutorial, we will be using jQuery, although any library such as BBC Glow can be used in the same sense.<\/p>\n\n

\/\/Start the namespace<\/span>\nvar<\/span> namespaceName =<\/span> (<\/span>function<\/span>(<\/span>$)<\/span>{<\/span>\n \n\t\/\/\/\/\/\/\/<\/span>\n\t\/\/Settings Object<\/span>\n\t\/\/\/\/\/\/\/<\/span>\n\tvar<\/span> settings =<\/span> {<\/span>\n\t\tvarOne:<\/span> 'All is working and groovy!!'<\/span>,<\/span>\n\t\tvarTwo:<\/span> 111<\/span>,<\/span>\n\t\tvarThree:<\/span> true<\/span>\n\t}<\/span>\n \n\t\/\/\/\/\/\/\/<\/span>\n\t\/\/Private Function<\/span>\n\t\/\/\/\/\/\/\/<\/span>\n\tfunction<\/span> private<\/span>(<\/span>)<\/span>{<\/span>\n                \/\/Alert to test it is all working<\/span>\n                alert<\/span>(<\/span>settings.varOne<\/span>)<\/span>;<\/span>\n\t}<\/span>\n \n\t\/\/\/\/\/\/\/<\/span>\n\t\/\/Public init<\/span>\n\t\/\/\/\/\/\/\/<\/span>\n\tinit =<\/span> function<\/span>(<\/span>)<\/span> {<\/span>\n\t\tprivate<\/span>(<\/span>)<\/span>;<\/span>\n\t}<\/span>\n \n\t\/\/Return the public functions<\/span>\n\treturn<\/span> {<\/span>\n\t\tinit:<\/span>init\n\t}<\/span>\n}<\/span>(<\/span>window.$)<\/span>)<\/span>;<\/span><\/pre><\/div><\/div>\n\n

To use this is the context of your document. Fire it off in a document ready function like this:<\/p>\n\n

$(<\/span>document)<\/span>.ready<\/span>(<\/span>function<\/span>(<\/span>)<\/span>{<\/span>\n        \/\/Fire off the init function<\/span>\n\tnamespaceName.init<\/span>(<\/span>)<\/span>\n}<\/span>)<\/span>;<\/span><\/pre><\/div><\/div>\n\n

And there you have it. Any thoughts, comment below and happy coding!<\/p>\n", "excerpt": "One of the most useful things I use in my job is a JavaScript namespace template. It is really useful for body-guarding variables and functions and writing well organised code. This is especially useful if you write extensive and complex programs for your website. A collection of un-organised jQuery plugins for example will result in [...]", "date": "2012-04-19 00:40:05", "modified": "2012-04-23 11:47:27", "categories": [ { "id": 7, "slug": "javascript", "title": "JavaScript", "description": "", "parent": 4, "post_count": 5 }, { "id": 8, "slug": "jquery", "title": "jQuery", "description": "", "parent": 4, "post_count": 4 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" }, { "id": 66, "type": "post", "slug": "detect-internet-explorer-and-ie-version-with-javascript-tutorial", "url": "http:\/\/hankchizljaw.co.uk\/tutorials\/detect-internet-explorer-and-ie-version-with-javascript-tutorial\/18\/04\/2012\/", "status": "publish", "title": "Detect Internet Explorer and IE Version with Javascript (Tutorial)", "title_plain": "Detect Internet Explorer and IE Version with Javascript (Tutorial)", "content": "

On the of the worst things any front end developer will have to do is fix their beautiful website to work with the black sheep of the browser world, Internet Explorer. The most annoying thing is that this black sheep still dominates the browser market world wide so we do morally have to at least make our websites work with IE as a minimum. We have always got to remember the code of a web developer,<\/p>\n

“We code a website for our clients and their clients, not for ourselves.”<\/p><\/blockquote>\n

So with this in mind, I have made a little snippet of JavaScript that should help you fix them horrible bugs.<\/p>\n

The first thing I can hear you all thinking is “What if the user doesn’t have JavaScript enabled?”<\/em>. For me personally, this isn’t an issue for two reasons:<\/p>\n

    \n
  1. So many sites are JavaScript dependent these days and so little users now have JavaScript disabled.<\/li>\n
  2. I work in a .NET environment in my day job in which a lot of the front end interaction with the server requires\u00a0JavaScript to be enabled.<\/li>\n<\/ol>\n

    So for the interests of this tutorial we will go with the assumption that JavaScript is on.<\/p>\n

    The Code<\/h2>\n

    The main thing this code does is check if we are using IE, then it uses a regular expression to parse the version number out of the user agent string. This then give is a nice integer to use in a conditional sense.<\/p>\n\n

    \/\/Internet Explorer<\/span>\nif<\/span> (<\/span>navigator.appName<\/span> ==<\/span> "Microsoft Internet Explorer"<\/span>)<\/span> {<\/span>\n    \/\/Set IE as true<\/span>\n    ie =<\/span> true<\/span>;<\/span>\n    \/\/Create a user agent var<\/span>\n    var<\/span> ua =<\/span> navigator.userAgent<\/span>;<\/span>\n    \/\/Write a new regEx to find the version number<\/span>\n    var<\/span> re =<\/span> new<\/span> RegExp(<\/span>"MSIE ([0-9]{1,}[.0-9]{0,})"<\/span>)<\/span>;<\/span>\n    \/\/If the regEx through the userAgent is not null<\/span>\n    if<\/span> (<\/span>re.exec<\/span>(<\/span>ua)<\/span> !=<\/span> null<\/span>)<\/span> {<\/span>\n        \/\/Set the IE version<\/span>\n        ieVersion =<\/span> parseInt(<\/span>RegExp.$1)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\nelse<\/span> {<\/span>\n    ie =<\/span> false<\/span>;<\/span>\n}<\/span><\/pre><\/div><\/div>\n\n

    As you can see, this is a pretty simple bit of code. The more observant of you may have also noticed that I have a Boolean flag set (ie = true\/false). This is a nice global Boolean that I can use to conditionality provide functionality to a user depending of their platform.<\/p>\n

    Now after that first snip of code we can start referring to the integer that is provided. For example, lets say I have loads of layout bugs in IE 7. A good idea would be to add a class of .IE7 to my body tag. This almost works how Modernizr does but you have much more control of what the returns are.<\/p>\n\n

    if<\/span>(<\/span>ieVersion <<\/span> 8<\/span>)<\/span>{<\/span>\n     $(<\/span>'body'<\/span>)<\/span>.addClass<\/span>(<\/span>'IE7'<\/span>)<\/span>;<\/span>\n}<\/span><\/pre><\/div><\/div>\n\n

    With CSS I can now fix those pesky bugs like this:<\/p>\n\n

    .IE7<\/span> .peskyBug<\/span> {<\/span>\n     \/* Insert my fix code here *\/<\/span>\n}<\/span><\/pre><\/div><\/div>\n\n

    I personally prefer this method than using conditional HTML statements. It means I have only one style-sheet which is generally better for client-side performance.<\/p>\n

    I am always up for a discussion so if you have any comments or better methods, let me know below.<\/p>\n", "excerpt": "On the of the worst things any front end developer will have to do is fix their beautiful website to work with the black sheep of the browser world, Internet Explorer. The most annoying thing is that this black sheep still dominates the browser market world wide so we do morally have to at least [...]", "date": "2012-04-18 23:50:58", "modified": "2012-04-23 11:50:54", "categories": [ { "id": 7, "slug": "javascript", "title": "JavaScript", "description": "", "parent": 4, "post_count": 5 }, { "id": 8, "slug": "jquery", "title": "jQuery", "description": "", "parent": 4, "post_count": 4 }, { "id": 4, "slug": "tutorials", "title": "Tutorials", "description": "", "parent": 0, "post_count": 7 } ], "tags": [], "author": { "id": 1, "slug": "hcj", "name": "Hank Chizl Jaw", "first_name": "", "last_name": "", "nickname": "Hank Chizl Jaw", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open" } ] }