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.

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.

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/”]