Saturday, May 21, 2011

Escaping Strings for jQuery Selectors

The possibility to get DOM elements via selectors is one of the most powerful features of jQuery. However, it's not so rare that you get problems because the id of the element you want to select or any other attribute, class or value relevant for the selection contains characters that are used by jQuery for the definition of the selector itself. In the jQuery documentation it's clearly defined which characters are considered as meta-characters for selectors and thus have to be escaped with a backslash (or better two backslashes if you're passing the value with a string literal):
"If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \\."

Unfortunately, jQuery doesn't offer any function to escape these characters within a string, so that you don't have to care about what characters you must not use when defining attributes of your markup.

So I've spent almost one hour playing (or better fighting) with regular expressions in order to create a general function to escape strings to use in jQuery selectors. So if you're an absolutely RegEx guru or just have the time to figure out the right regular expression to use for this purpose, then you're probably not so interested in the following function. Otherwise, you should really try it out! ;)

  function jQuerySelectorEscape(expression) {
      return expression.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&');
  }

The function inserts a backslash before all meta-characters for jQuery selectors within a string.
Here's an example:
<div id="my#div$has[some]strange*characters">My DIV</div>
<br/>
<script type="text/javascript">
  var selector = '#' + jQuerySelectorEscape('my#div$has[some]strange*characters');
  document.write('selector = ' + selector);
  document.write('<br/><br/>');
  document.write('DIV content = ' + jQuery(selector).html());
</script>
Output:
My DIV

selector = #my\#div\$has\[some\]strange\*characters

DIV content = My DIV

I hope you can avoid some headaches with this function. If there's any question or problem with it, leave a comment below and I'll try to help you out.

Wednesday, May 11, 2011

Android@Home

Apparently my almost utopian prediction in my last post of controlling everything at home with an Android device could become true sooner than expected.

Google teases Android@Home in the annual Google I/O Conference of 2011. As mentioned in this news article:

This (Android@Home) is an experimental framework designed to encourage developers to get a headstart on developing apps and software, making the "home an Android accessory". The team demoed a device which could turn lights on and off, control alarm clocks or home appliances such as fridges and cookers via your android device - all of which is planned to be a long-term development, probably end of 2012.

So, I'm looking forward to having my breakfast ready when I get up from bed in the more or less near future... :)

Saturday, May 7, 2011

Android App: Unified Remote

I just wanted to share this amazing new app I've just found: Unified Remote


It basically transforms your Android smartphone into a remote control for any type of computer that has the Unified Remote Server installed. In addition to general input devices like mouse and keyboard, it's possible to control a lot of other stuff in your PC. You can browse your whole file system and open files directly from the device, control Windows Media Player, iTunes or the like and control Power Point and other Slide Show applications. It's also possible to view the Task Manager and kill tasks or even shut down or hibernate your computer!

The app connects to the PC via UDP and TCP. It's also possible to connect via Bluetooth, however I didn't find out how to do it.

Anyway, the possibilities are enormous and for me this is definitely the future of mobile devices. Sooner or later, it won't be necessary to have a remote control for each device at home. Everything (and I don't mean only TV's and HI-FI systems) could be controlled with a smartphone. Imagine you being waked up by your alarm clock in your smartphone, which automatically triggers your toaster and coffee machine, so that your breakfast is ready before you've even stood up from your bed... Well, I think this is the very near future. ;)

Until then check out the app and comment what you think about it...