parentNode.org

The building blocks of a solid frontend.

Obfuscated javascript challenge

Posted in JavaScript by Chris Benjaminsen on the February 9th, 2007

While solving the small “hacker” challenge hackordie I thought it would be fun to try to write my own two expert javascript challenges.

The result was four for rather strange javascript blocks, where two where actually challenges and two where just me playing around.

(more…)

The Zen attachment

Posted in Usability, Accessibility, Browsers, CSS, JavaScript by Munter on the January 30th, 2007

This is a small story about the beauty of simple solutions. It is the story about an elaborate problem set where each solution gets more complex, and doesnt really solve the main issue. In the end a stroke of genious solves the problem with only a 16 byte size increase. Keep It Simple Stupid

(more…)

Javascript Browser Detection Revisited

Posted in Browsers, JavaScript by Munter on the July 14th, 2006

You’ve all seen them before. Javascript browser detection scripts. There are extremely large and ugly ones out there, ignoring the fact that browser detection in itself is an ugly thing having to resolve to. Our suggestion is based on not trusting the user agent string delivered by the browser. Instead we use a combination of object detection and bad voodoo to be 100% sure what browser and what version is served. Updated for IE7!

(more…)

Default Arguments in JavaScript Functions

Posted in JavaScript by fatbrain on the June 16th, 2006

I often run into people who need (for reasons that are beyond me) to be able to give arguments in a javascript function default values. Kinda like what you have in C/C++:

void foo(int a, int b = 42) { ... }

I felt an urge to do it, so I sat down for a few minutes, trying to conjure something nifty that would be intuitive enough for even me to use.

The solution I came up with, and that hopefully will put an end to the I-need-default-arguments-in-javascript rant for good.

The solution is quite simple, and very intuitive. It may have some shortcomings but keep in mind I didn’t explicitly write it for you. You could probably modify my solution to fit your needs anyway.

I’ve seen alot of developers (including myself, long time ago) using this pattern as a workaround for the lack of built-in support for default-arguments.

function foo(a, b)
{
  a = typeof(a) != 'undefined' ? a : 42;
  b = typeof(b) != 'undefined' ? b : 'default_b';
  ...
}

Which most developers probably find sufficient, I don’t.

The solution may look scary at a quick first glance, but bear with me. I’ll start by writing the framework-code. The code that will be required for this thing to work.

Function.prototype.defaults = function()
{
  var _f = this;
  var _a = Array(_f.length-arguments.length).concat(
    Array.prototype.slice.apply(arguments));
  return function()
  {
    return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(
      _a.slice(arguments.length, _a.length)));
  }
}

See that wasn’t so scary :).

In order for this to work you have to declare you functions in a special way. There are basicly two ways of declaring a function.

function foo(a, b)
{
  ...
}

Is identical to (apart from the lexical difference, and some other minor things)

var foo = function(a, b)
{
  ...
}

In order for this solution to work you have to declare all functions on which you wish to have default-arguments the latter way.

Usage

var foo = function(a, b)
{
  ...
}.defaults(42, 'default_b');

Is identical to the first code-block but without adding any code in the function-body.

Example

var bar = function(a, b)
{
}.defaults('default_b');

bar();
// a = undefined, b = 'default_b'

bar(1);
// a = 1, b = 'default_b'

bar(1, 'some_value');
// a = 1, b = 'some_value'

Inline ActiveX object activation workaround.

Posted in Internet Explorer, Flash, JavaScript by Chris Benjaminsen on the April 26th, 2006

!! The slution described on this page does not work in IE7 please stay tuned! !!

If you work with ActiveX elements in the browser, you properly already know that these elements now require activation before they can receive keyboard and mouse input. I will in this article shortly describe why Microsoft had to do this change, how Microsoft suggest you solve this issue and finally provide you with my personal solution that allows you to use ActiveX elements inline without requiring activation.

(more…)

Working with the Cursor Position

Posted in JavaScript by white on the February 9th, 2006
One question, I’m getting asked almost once a day is:
“Is it possible to insert text at the current cursor position inside a textarea ?”

The answer is yes, for the following browsers

  • Internet Explorer (IE)
  • Any Gecko based (e.g. Firefox)
  • Opera 8+

Any other browser does not support this functionality.
So if you want to keep your page 100% working for everyone out there, you should stop reading here !

(more…)

Using logic to minimize cross browser scripts

Posted in JavaScript by Munter on the February 1st, 2006

Javascripters often find themselves trapped between different implementations of the same standard. In the last millennium browser detection scripts where the big thing. Since then we have luckily progressed a bit in turns of crossbrowser compatibility. Not so much because of increased compatibility, but rather because of some nifty tricks in javascript that allow us not to worry about browsers.

(more…)