Tag: HTML

Breakpoint JS – load external javascript for different browser breakpoints

idea

Live Demo: http://hasinhayder.github.io/BreakPoint/

Breakpoint helps you to define multiple breakpoint ranges and load external javascript files when someone resize their browser window to those breakpoints. Beside loading external scripts, Breakpoint also triggers an event whenever browser width falls in any of those predefined breakpoint ranges.

Using Breakpoint
Using BreakPoint is pretty simple. Include jQuery in your web application and then include jquery.breakpoint.js. Now you can define multiple breakpoint ranges and initialize like this

[sourcecode language=”javascript”]
$(document).ready(function(){
$.BreakPoint({
breakpoints:{
mobile:{max:480,load:true},
default:{min:480,max:1024, load:true},
wide:{min:1024,max:1200,load:true},
superwide:{min:1200,load:true},
}
});
});
[/sourcecode]

In the example above, we have defined four different breakpoints. The first one mobile will be used whenever browser is resized from 0px to 480 pixel (all inclusive). The next breakpoint, default will be used whenever browser is resized between 480px to 1024px (all inclusive), and so on.

Now, whenever a breakpoint is reached, BreakPoint jQuery Plugin will load an external javascript file with the same name of the breakpoint. For example, when browser is resized to 400px it will fall under mobile and now an external file named mobile.js will be loaded and executed.

By default BreakPoint will look for external javascript files in the js/ folder in the current path. But you can change this by passing a parameter named prefix, like this

[sourcecode language=”javascript”]
$(document).ready(function(){
$.BreakPoint({
prefix:"scripts/",
breakpoints:{
mobile:{max:480,load:true},
default:{min:480,max:1024, load:true},
wide:{min:1024,max:1200,load:true},
superwide:{min:1200,load:true},
}
});
});
[/sourcecode]

Now BreakPoint will load external js files from the scripts/ folder.

Breakpoint Parameters
prefix: By default, prefix is set to js/ but you can change it to anything you want.
breakpoints: for each breakpoints, there are three paramaters you can pass which are “min”, “max” and “load”. By default min is set to 0, max is set to 9999 and load is set to false. The load parameter is an important one if you want to load external javascript files whenever a breakpoint is reached. If load is set to true, external files will be loaded, otherwise not.

BreakPoint Events
Whenever a breakpoint is reached, BreakPoint plugin will trigger a window event named breakpoint. The event object will have an attribute named breakpoint which will contain the name of the breakpoint that has been reached. Here is an example how you can add an event listener to this breakpoint event

[sourcecode language=”javascript”]
$(document).ready(function(){
$(window).bind("breakpoint",function(e){
if(console)
console.log(e.breakpoint);
});
});
[/sourcecode]

That’s mainly it. Enjoy! Check out the live demo at http://hasinhayder.github.io/BreakPoint/

Detecting if a DOM element has visible scrollbar.

Webkit browsers, specially in Mac osx hides the scrollbar intelligently when not in use. This feature improves the look-n-feel. But for the designers, sometime this can be a pain because they may need to alter the designs based on the visibility of scrollbar.

Here goes two simple jQuery plugin which can detect the visibility of scrollbar for a given DOM element

For Horizontal Scrollbar
[sourcecode language=”javascript”]
(function($) {
$.fn.hasHorizontalScrollBar = function() {
return this.get(0) ? this.get(0).scrollWidth > this.innerWidth() : false;
}
})(jQuery);
[/sourcecode]

For Vertical Scrollbar
[sourcecode language=”javascript”]
(function($) {
$.fn.hasVerticalScrollBar = function() {
return this.get(0) ? this.get(0).scrollHeight > this.innerheight() : false;
}
})(jQuery);
[/sourcecode]

You can later use it like this
[sourcecode language=”javascript”]
if($("#element").hasVerticalScrollbar()){
//do whatever you’d like to
}
[/sourcecode]

Source & Idea: This SO Discussion 🙂

Getting comfy with PhpStorm – one of the best IDEs so far!

Beside the frequently asked question “Which Framework is best?” this one is the top in category – “Which one is the best IDE”. People asks this question in morning, afternoon and evening. Every-time, everywhere. I am a big time fan of Netbeans and I left it a few days after it’s release of 6.7. It was so good, heavenly, yummy but I had to leave this old pal because of it’s extreme hunger to the available resources. There was only one thing that time which could compete with the resource hungry netbeans – that is FireFox. But hey, FF is not your IDE after all. Anyway, back to my point – I left Netbeans and started using PhpStorm. I have a company license and I am glad that I made this move. I am not going to preach PhpStorm in rest of this article, but what I will do is sharing my experience with PhpStorm 🙂

1. PhpStorm is fast: Not like a Bugatti Veyron beside your old 4 wheelers, but it’s fast. I was told that 64 bit version is even faster but that was not true in my macbook pro running snow leopard. rather I found that 64 bit version is slightly sluggish and more resource extensive. Whatever, when you make a jump from Netbeans to PhpStorm, you will really feel the speed of everything and if you keep an eye over your system memory, you will surely say “wow!”.

2. The best Javascript and X/HTML Intellisense: I am a javascript developer for almost 8 years by now. I have tried eclipse, some standalone components which could also run as eclipse plugin and Aptana. Aptana was good – I was using aptana mostly because of it’s auto-completion capability of javascript. But I really HATE eclipse. Why? – I will write a separate blog post on that. Now Netbeans has some capability of intellisense for Javascript but when it comes to jQuery (remember, I left NB at it’s 6.7th release) it was frustrating. PhpStorm really is beautiful for this. Just amazing! The format, the auto suggestion, the code completion everything works just perfect. This is one of the main reason why I am sticking with PhpStorm

3. Version Controlling: Ya ya I know, NB got that too. I am not a Netbeans hater, rather I love it. I love how amazingly it changed my world of coding by charging me nothing – zip, zilch nada! So it is merely a selling point for making a move from NB to PhpStorm – but the thing is, support for different VCS in PhpStorm is as good as like the other parts of it. It has fantastic support for Git and Svn, beautiful tools for merging and fixing the conflicts, refactoring and what not. I was feeling at home and to be honest, migrating from NB to PhpStorm didnt feel like I came to the land of aliens just for this. Without integrated support for VCS it would be impossible for me to stay with PhpStorm.

4. Less hungry: Though Steve Jobs said to stay hungry and foolish but please note that he said it only for humans, not to IDEs :P, lol! Ok, so PhpStorm is less hungry to CPU and Memory than NB. I just wish Firefox will be a performer like this one day 🙂 (different story again, sorry I am distracted with too many things at once)

5. Less Costly and a Friendly company: Phpstorm is a developer friendly company. I am using the licensed copy of Zend Studio since 2005 (I got that for free because of being a ZCE and because of winnign some competitions over time) but heck! Zend Studio is costly. That’s fine, Zend Studio is the PHP company and knows inside out of making a PHP IDE. But trust me, PhpStorm is not an alien thing at all – you will not feel “vim” with this excellent tool. PhpStorm can help you code PHP, HTML, CSS and JS with maximum comfort. Yet, it costs much less than other similar IDEs with similar feature set. And what did I mean by “Developer Friendly”? PhpStorm team listens to you. Most of the time you mail them for a discount, they give you some sort of discount without expecting anything from you (I love you, PhpStorm team for this). Even Elite in the class Zend Studio gives some discount some time. But try the same with Elite of the Elites like Coda. They wont take their time to say “Go, fuck yourself!” – I hate Coda, seriously! So rude!

Anyway – that’s a long story. I am very happy with PhpStorm and has recently upgraded my IDE from 2.x to 3.x with their one year full update support (even with major versions :D, Zend you should really think about that with Zend Studio) and living happily ever after. Love you PhpStorm, I really do!.

Get your 30 days trial version of PhpStorm from http://www.jetbrains.com/phpstorm and feel a whole different level of satisfaction 🙂 And surely, dont forget to check NetBeans too, that’s a beautiful piece of tool.