8 Feb 2011 Ferado

jQuery Default / JavaScript Default Options

This blog is more for myself than any of you readers out there. Every so often I need to have an object of some kind or a function that has its own defaults. Here’s how to do it using jQuery:

[cc lang=”javascript”]

// function
function myFunc(options) {
var defaults = {option1: ‘val’, option2: ‘val2’};
var settings = $.extend({}, defaults, options);
// do something
}

// or, an object
var myObj = {
init: function(options) {
var defaults = {option1: ‘val’, option2: ‘val2’};
var settings = $.extend({}, defaults, options);
}
};

[/cc]

Conclusion

It’s easier to remember how to do things when you practice them. This was my practice at setting JavaScript defaults. See the reference link for more, it’s a good one.

30 May 2010 Ferado

C# 4.0 & Optional Parameters

When writing a method in .NET (C# or VB), you typically have to overload that method if you want to provide default or optional parameters. Now, thanks to C# 4.0 and VB 10, you can use optional parameters.

For example, prior to C# 4.0, you had to do the following:

[cc lang=”c#”]
// Assume we’re using a class and the following stuff is from our class

// no parameters
public void myMethod()
{
myMethod(“optional”);
}

// one parameter
public void myMethod(string str)
{
// STUB
}
[/cc]

As you can see from the preceding code, you need to type more than one function. To each their own. Some people like doing this, some people don’t. I like how it works in PHP:

[cc lang=”php”]
function myFunc($optional=”hello world!”) {
// STUB
}
[/cc]

Even better, Python let’s you do this:

[cc lang=”python”]
def myFunc(str1=”something”, str2=”something else”):
print str1, str2

# usage:
myFunc(str2=”Something new”)

# prints out
something Something new
[/cc]

In C# 4.0, you can do something similar to the way PHP does it, like so:

[cc lang=”c#”]
public void myMethod(int i = 1, double awesome = 3.0)
{
// STUB
}
[/cc]

Conclusion

You’ve now just learned how in the new .NET (check this post’s date) allows you to use optional parameters to cut down on your code so that you don’t have to overload your methods anymore, unless you want to. You’re going to need this in your toolbox. Note, please use overloading when you need to. There might be a time when you need to overload in order to keep your code looking cleaner. The choice is yours. Use responsibly.

17 Jan 2010 Ferado

How to check if your popup was blocked using JavaScript

When I researched how to get pop ups to not be blocked using flash, in that research, I found out how to check and see if a popup was blocked or not. It’s such a simple solution I don’t know why I haven’t consciously noticed it before. I’ve created many popups in the past, just never thought about how I would check if a popup was blocked.

The Solution

Using JavaScript, put something like this in your code:
[cc lang=”javascript”]
// this is sample code so replace
// wurl = the url you want opened
// wname = name given to the window so you can reference it again programmically
// wfeatures = optionally you can give it scroll bars, make it resizeable, etc.
var win = window.open(wurl, wname, wfeatures);
if(win == null || typeof(win) == “undefined” || win.location.href == ‘about:blank’) {
alert(“Please enabled popups for this site to continue.”);
}
[/cc]

That’s it! That’s all you need to do to test if someone has blocked your popup. Now you need to decide if it’s worth the hassle of working with popups and how to handle when a popup is blocked.

The three simple ways around using popups are:

  1. Use target=”_blank” on your anchor tag
  2. Use a modal popup, which is just a layer within the DOM
  3. Use inline code that slides down, up or sideways

Remember that you should never automatically open a new window for a user. That’s just rude and unethical these days. Use something inline instead. Only open a new window after a “click” event has been fired. Automatic popups are evil.

3 Jan 2010 Ferado

How to embed animated GIFs into your Flash or AIR app

I’m working on a web page thumbnail generator and a new blog post (a good one this time) about web page thumbnail generation. A topic for another day; however, when making the preloader for it…I don’t like the flash preloaders that you see every where, and I thought giving progress back wouldn’t be worth it because how am I going to know how to calculate the percentage done. You know how it says 60%, 70%, 80% percent in normal preloaders? Well, in this situation it wouldn’t be accurate to do something like that because I’m doing a whole bunch of different calculations, etc.

So what did I do? I thought, “Hey, why not just use an animated gif, like what I see on ajaxload.info?” I get back to coding and I load in my gif right? Wrong.
Continue reading “How to embed animated GIFs into your Flash or AIR app” »

31 Dec 2009 Ferado

The Best jQuery Lightbox Scripts

I found a site for you that lists some of the best jQuery Lightbox Scripts. If you don’t already know, lightbox scripts are pretty much just javascript files that create modal pop ups. A lightbox can be used to show images, videos, and/or web pages inline without having to open a new browser window.

Keep an eye out for the colorbox, it’s a good one.

[source label=”Visit Source” href=”http://woorkup.com/2009/12/29/jquery-lightbox-scripts/”]

18 Aug 2009 Ferado

Using PHP to format phone numbers

PHP is great. It complements JavaScript wonderfully. You may have already formatted the phone number on the front end, but what about on the backend? There comes a time when you need to use php to format a phone number. I use this function in my projects all the time.

PHP Format Phone Number Function

[cc lang=”php”]

function format_phone($country, $phone) {
$function = ‘format_phone_’ . $country;
if(function_exists($function)) {
return $function($phone);
}
return $phone;
}

function format_phone_us($phone) {
// note: making sure we have something
if(!isset($phone{3})) { return ”; }
// note: strip out everything but numbers
$phone = preg_replace(“/[^0-9]/”, “”, $phone);
$length = strlen($phone);
switch($length) {
case 7:
return preg_replace(“/([0-9]{3})([0-9]{4})/”, “$1-$2”, $phone);
break;
case 10:
return preg_replace(“/([0-9]{3})([0-9]{3})([0-9]{4})/”, “($1) $2-$3”, $phone);
break;
case 11:
return preg_replace(“/([0-9{1})([0-9]{3})([0-9]{3})([0-9]{4})/”, “$1($2) $3-$4”, $phone);
break;
default:
return $phone;
break;
}
}

// usage
$phone = ‘111 111 1111’;
$phone = format_phone(‘us’, $phone);
echo $phone;

[/cc]

Continue reading “Using PHP to format phone numbers” »

18 Apr 2009 Ferado

Simple copyright year function

I still see on a lot of sites the copyright date being outdated. Shoot, it was even outdated on mine. How embarrassing. Easy fix though. Something I do at work all the time, but since I’m not getting paid for this site, it wasn’t done.

Now, let’s forget the past and move onto the future.

The solution (in PHP):

[cc lang=”php”]
function copyright_year($year = ‘2009’, $sep = ‘-‘) {
$current_year = date(‘Y’);
return ($year == $current_year) ? $year : $year . $sep . $current_year;
}
[/cc]

OR, for those that need more lines (I do too. I like to be able to READ my code):

[cc lang=”php”]
function copyright_year($year = ‘2009’, $sep = ‘-‘) {

$current_year = date(‘Y’);

// compare year that is passed in with current year
if($year == $current_year) {
return $year;
}

// I hate writing the word “else”
return $year . $sep . $current_year;

}
[/cc]

I’ve also made it easy for you to download the source code, just in case you like things REALLY simple! I do, don’t be shy.

DOWNLOAD SOURCE CODE