Se afișează postările cu eticheta JavaScript. Afișați toate postările
Se afișează postările cu eticheta JavaScript. Afișați toate postările

miercuri, 8 decembrie 2010

JavaScript from Null: Utility Functions and Debugging

 Topic: JavaScript Difficulty: Basix Format: 24 Minute Video

JavaScript University continues as we develop our first utility function that will allow us to filter and retrieve only the unique values from an array. Along the way, I’ll also teach you how to use the excellent Firebug to debug your code.

var unique = function(origArr) { var newArr = [], origLen = origArr.length, found, x, y; for ( x = 0; x < origLen; x++ ) { found = undefined; for ( y = 0; y < newArr.length; y++ ) { if ( origArr[x] === newArr[y] ) { found = true; break; } } if ( !found) newArr.push( origArr[x] ); } return newArr;};// Test it outvar myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie'];myarray = unique(myarray);console.log(myarray); // jeffrey, allie, patty, damon, zach

So, with this lesson out of the way, you now know how to build your own helpful utility functions. I hope you enjoyed today's video tutorial!


View the original article here

marți, 23 noiembrie 2010

5 Free Copies of Test-Driven JavaScript Development

 In addition to donating an excerpt of his excellent new book to Nettuts+, Christian and Pearson books have also been generous enough to provide us with five digital copies of the book to give away.

Despite the fact that the JavaScript language has grown in leaps and bounds over the last few years — in both features and popularity — it’s still one of the most least tested languages. Why do you think that is? Leave a comment and let us know your honest opinion. On November 22nd (EST), we’ll choose five winners, and send them a free copy of the book! That’s all there is to it.



Tired of console.log-ing your way out of trouble? Weary from constant fear of cross-browser issues? Scared of making even the slightest change to production code? Test-Driven JavaScript Development teaches you how to solve those issues and more using unit tests and TDD. Rich with examples, the book provides a solid foundation on automated testing, a test-driven walk-through of JavaScript’s unique qualities, and five practical examples of building solid, reusable, cross-browser JavaScript using TDD.


View the original article here


High Conversions and High Payouts = Huge Money! Visit www.profitpal.com for affiliate tools or email us at contact@profitpal.com for more info.


Check it out!

sâmbătă, 20 noiembrie 2010

Quick Tip: Detect CSS3 Support in Browsers with JavaScript

Isn’t it fun that we get to play with the latest CSS techniques, like shadows and transitions? There’s only one problem: how do we compensate, or more importantly, detect the browsers that do not support them? Well, there’s a few solutions. In this tutorial and screencast, though, we’ll create a JavaScript function that will accept a CSS property name as its parameter, and will return a boolean, indicating whether or not the browser supports the passed property.


Let’s begin by determining how we want to call our function. We’ll keep things simple here; something like the following should do the trick:

if ( supports('textShadow') ) { document.documentElement.className += ' textShadow';}

That should be the final function call. When we pass a CSS property name to the supports() function, it’ll return a boolean. If true, we’ll attach a className to the documentElement, or . This will then provide us with a new `class` name to hook onto, from our stylesheet.


Next, we’ll construct the supports() function.

var supports = (function() {})();

Why aren’t we making supports equal to a standard function? The answer is because we have a bit of prep work to do first, and there’s absolutely no reason to repeat those tasks over and over every single time the function is called. In cases like this, it’s best to make supports equal to whatever is returned from the self-executing function.


To test whether or not the browser supports specific properties, we need to create a *dummy* element, for testing. This generated element will never actually be inserted into the DOM; think of it as a test dummy!

var div = document.createElement('div');

As you’re probably aware of, there are a handful of vendor-prefixes that we can use, when working with CSS3 properties:


Our JavaScript will need to filter through those prefixes, and test them. So, let’s place them in an array; we’ll call it, vendors.

var div = document.createElement('div'), vendors = 'Khtml Ms O Moz Webkit'.split(' ');

Using the split() function to create an array from a string is admittedly lazy, but it saves a handful of seconds!


As we’ll be filtering through this array, let’s be good boys and girls, and cache the length of the array as well.

var div = document.createElement('div'), vendors = 'Khtml Ms O Moz Webkit'.split(' '), len = vendors.length;

The prep work, above, is static, in nature, and doesn’t need to be repeated every time we call supports(). This is why we only run it once, when the page loads. Now, let’s return the function that will actually be assigned to the supports variable.

return function(prop) {};

The beauty of closures is that, even though supports() is equal to that returned function, it still has access to the div, vendors, and len variables.


The immediate test: if the passed property is available to the div‘s style attribute, we know the browser supports the property; so return true.

return function(prop) { if ( prop in div.style ) return true;};

Think of, say, the text-shadow CSS3 property. Most modern browsers support it, without the need for a vendor prefix. With that in mind, why filter through all of the prefixes if we don’t need to? That’s why we place this check at the top.


You’re likely used to typing CSS3 property names, like so: -moz-box-shadow. However, if, in Firebug, you review the style object, you’ll find that it’s spelled, MozBoxShadow. As such, if we test:

'mozboxShadow' in div.style // false

False will be returned. This value is case-sensitive.

Case Sensitive

This means that, if the user passes boxShadow to the supports() function, it’ll fail. Let’s think ahead, and check if the first letter of the argument is lowercase. If it is, we’ll fix the error for them.

return function(prop) { if ( prop in div.style ) return true; prop = prop.replace(/^[a-z]/, function(val) { return val.toUpperCase(); });};

Regular expressions to the rescue! Above, we’re checking if there is a single lowercase letter at the beginning of the string (^). Only on the condition that one is found, we use the toUpperCase() function to capitalize the letter.


We next need to filter through the vendors array, and test if there’s a match. For instance, if box-shadow is passed, we should test if the style attribute of the div contains any of the following:

MozBoxShadowWebkitBoxShadowMsBoxShadowOBoxShadowKhtmlBoxShadow

If a match is found, we can return true, because the browser does, indeed, provide support for box shadows!

return function(prop) { if ( prop in div.style ) return true; prop = prop.replace(/^[a-z]/, function(val) { return val.toUpperCase(); }); while(len--) { if ( vendors[len] + prop in div.style ) { return true; } }};

Though we could use a for statement to filter through the array, there’s no real need to in this case.

The order isn’t important while statements are quicker to type, and require fewer characters There’s a tiny performance improvement

Don’t be confused by vendors[len] + prop; simply replace those names with their real-life values: MozBoxShadow.


But, what if none of those values match? In that case, the browser doesn’t seem to support the property, in which case we should return false.

while(len--) { if ( vendors[len] + prop in div.style ) { return true; }}return false;

That should do it for our function! Let’s test it out, by applying a className to the html element, if the browser supports, say, the text-stroke property (which only webkit does).

if ( supports('textStroke') ) { document.documentElement.className += ' textStroke';}

With a class name that we can now hook onto, let’s try it out in our stylesheet.

/* fallback */h1 { color: black;} /* text-stroke support */.textStroke h1 { color: white; -webkit-text-stroke: 2px black;}var supports = (function() { var div = document.createElement('div'), vendors = 'Khtml Ms O Moz Webkit'.split(' '), len = vendors.length; return function(prop) { if ( prop in div.style ) return true; prop = prop.replace(/^[a-z]/, function(val) { return val.toUpperCase(); }); while(len--) { if ( vendors[len] + prop in div.style ) { // browser supports box-shadow. Do what you need. // Or use a bang (!) to test if the browser doesn't. return true; } } return false; };})();if ( supports('textShadow') ) { document.documentElement.className += ' textShadow';}

For a more comprehensive solution, refer to the Modernizr library.


View the original article here


Business plan to open a coffee shop, written by an experienced shop owner, worker and entrepreneur. Includes many bonuses and extras including email and personal consult.


Check it out!