Search This Blog

Wednesday, January 27, 2010

Cross-Browser: Getting/Setting the Class Attribute of an Element Using JavaScript

It is common knowledge that getting and setting the class of an element in Internet Explorer requires a bit of a work-around from the standard JavaScript solution that works with all other browsers.

The Usual Way

For example, using setAttribute to set the class requires two different values depending on whether the browser is Internet Explorer (className) or all others (class):
// a conditional statement specifies class or className
var klass = (isIE) ? 'className' : 'class';
element.setAttribute(klass, 'new_class');
The same is also true when attempting to get the class value with getAttribute:
element.getAttribute('className'); // for IE
element.getAttribute('class'); // everybody else
But what if you could both get and set the values on all browsers without any conditional logic? Apparently, you can.

A Better Way

The following solution works in all versions of IE from at least 6 onward, Firefox 3, Chrome, Safari, and Opera 10. I haven't tried it on Konqueror or any of the Linux browsers yet, but I would be surprised if it did not work there equally well.
element.className; // get class(es)
element.className = 'new_class'; // (re)set to single class

// or, if you don't want to tamper with any existing
// classes: note the space before the new class name
element.className = element.className + ' new_class';

// examples
if (/new_class/.test(element.className) { // test
    element.style.border = 'solid 1px #555';
}

if (someVariable == someValue) { // add "new_class" class
    element.className = element.className + ' new_class';
}
I have created a simple example page. In this case, the script toggles a show and hide class on table rows. Just download a copy from Google Docs, open in your litany of browsers, and enjoy. :)

Thursday, December 17, 2009

$dom2mootools: MooTools JavaScript Element Construction / Adoption Just Got Easier!

$dom2mootools

Overview: $dom2mootools drastically speeds up the process of JavaScript element construction when using the MooTools framework. Given any element node, it produces MooTools element constructors and should perform nearly seamless parent-child adoption (including a final adoption at the end and a reminder to reassign to your own root node).

Accreditation: Idea suggested by Jessie Yeagle and implemented by Eric Knickerbocker, both developers at Intuitive Web Solutions.

Creation Date: November 20, 2009. Last updated June 23, 2010.

URL: http://mrrena.blogspot.com/2009/12/dom2mootools.html.

Simply paste the HTML source code you want
to convert into the text box below and click Submit.

Input

Check to flatten div HTML.

Output

Monday, October 19, 2009

"Failed To Find Flength File": Uber-Uploader Error SOLVED


"Failed to find flength file."

I moved a new application we're developing from the development server onto the one that we hope to release live soon. As part of this package, we make use of Uber-Uploader and experienced no problems with the dev server, which was set up identically to the soon-to-be-live configuration. However, I kept getting a "Failed to find flength file" error on the new server.

I first consulted the page from Uber Uploader entitled Flength File Help "Failed To Find Flength File." That was clearly not the problem—I did not even have the mod_security module for Apache configured. I then ended up in a discussion forum related to the XUpload solution. Still no joy, though looking back, some people may have been on the right track, just not able to clearly communicate the solution.

In short, it was a file permissions issue. As most of you reading these words probably know, Linux has file permission settings and your Perl/CGI uploader script needs to chmod-ed to 755 or similar—it needs to be fully executable, in other words. No doubt the same issue would be true of an MS server with permissions set as well.

Don't know how to set script permissions? Most FTP programs offer that functionality, including the open-source program FileZilla—right click the Perl upload file, as shown below, and set your permissions to fully executable:

Wednesday, July 08, 2009

"EntityRef: expecting ';' at line 1": XML Error SOLVED


"EntityRef: expecting ';' at line 1"

I was using the Uber Uploader script for a project, and ran into the error above in the Firebug console. In my case, I had been passing a URL with a query string that needed to have HTML entities for the ampersands.

In other words, the following URL:
http://somesite.com/file.php?c=1&d=2&e=3
was creating an error in the XML file. The ampersands had to be encoded, which solved the error:
http://somesite.com/file.php?c=1&d=2&e=3
Wikipedia has a list of all the characters that must be encoded when using XML. I list them below with the symbol followed by the HTML entity you will need to use to replace it with. If you are receiving the same error (or the error "Reserved XML Name"), you most likely need to make sure that these symbols are encoded (and with the latter error, watch out for deadly white space and newline characters).
Symbol:HTML Entity:Description:
""(double) quotation mark
&&ampersand
'' or "apostrophe (= apostrophe-quote)
<&lt;less-than sign
> &gt;greater-than sign

Note that if you are using PHP, htmlspecialchars and htmlspecialchars_decode should give you everything you need for XML; for JavaScript, you'll want a custom solution such as this one.

Thursday, April 09, 2009

Trouble getting audio and video to sync properly in Cam Studio?

The article below was written by my friend Greg Rubottom.

Cam Studio is a great free desktop recording program for Windows. However, unless you use the proper settings for video and audio (see images below) you will end up with your video and audio out of sync in the final AVI file.

Audio:
Video:

I have found little information online about this problem, and what little I did find has proven to be less than accurate.

So, I created the following YouTube video to show you what the proper settings should be to maintain audio and video sync and to ensure that your Cam Studio files will be viewed in HD on YouTube.


Related post: My Favorite Free Video Converter, also by Greg.

My Favorite Free Video Converter

The recommendation below was written by my friend Greg Rubottom.

I searched and searched the web for a decent free video file format converter. I have no idea how many I must have tried, but it was a significant number. In the end I think it is no contest.

Any Video Converter is hands down my favorite. There are two versions: a free version, which is only available on Windows, and then a professional version, which has a few more nice options on Windows and is also available for Mac.

Give it a try, I do not think you will be disappointed: free version, $29.95 version.

Related post: Trouble getting audio and video to sync properly in Cam Studio?, also by Greg.

Thursday, March 05, 2009

Speed up Torrent and Peer to Peer (P2P) Downloads on Vista and other Windows Platforms


As a security precaution, Windows XP and Vista and the new Windows 7 limit the number of so-called "half-open" outbound TCP connection attempts to 10. Normally, that poses no problems, but if you do any downloading with torrent files or other peer-to-peer (P2P) networking, that can significantly slow your downloads.

Many XP users have relied on the lvllord.de patch for their solution, but that only works for XP and it modifies the tcpip.sys file, which has been known to have disastrous results for Vista users. Fortunately, there is a simple and elegant solution that works not only on XP (and better than the patch), but on Vista and 7 as well.

TCP-Z is a small GUI program for Windows that you can use to change the number of allowed half-open connections on the fly (up to 251) without restarting the computer (click image for full-size view):

To find out more about this program—and most importantly to download a copy for yourself—just head over to the TCP-Z Network Monitor site.

Note: To open the downloaded file, you may need a copy of the excellent and free 7-Zip (which in my opinion works better than the native Windows zip program), as the native program does not recognize the .7z file extension. (7-Zip recognizes its own extension and many others, including all the standards.)