Detecting mobile devices with Javascript

Today at work we were struggling with a way to detect the iPad and similar devices without relying on the browser user agent string.

We ended up checking the value of window.onorientationchange like so:

function is_mobile_device() {
    if (typeof window.onorientationchange != "undefined") {
        return true;
    }
    else {
        return false;
    }
}

This function will return false in all current desktop browsers because typeof window.onorientationchange is “undefined.” On the iPad and other mobile devices it will return a type of “object.”

What other ways are you using to detect mobile devices?

Mount your ntfs drive on boot in Ubuntu

So you’ve got an NTFS drive you want mounted when your Ubuntu machine boots? Time to pull up your favorite editor and modify /etc/fstab.

This is what I ended up with:

/dev/mapper/sil_aiaiahddacai1 /media/Storage ntfs-3g defaults,gid=1000,uid=1000,locale=en_US.UTF-8 0 0

Replace the gid and uid values with the ids of your group and user. Run id to get these values.

I also had to make sure I created the mount point, otherwise it would fail:

$ sudo mkdir /media/Storage

Anything I’m missing? So far it seems to work just fine.

upaste

After a brief and torrid affair with Fedora, I gave up and threw Ubuntu 10.04 on my home desktop. One thing that I missed from Fedora was the fpaste utility. This handy little command-line tool lets you paste the results of your shell commands to fpaste.org, a semi-official Fedora pastebin.

After realizing that pastebin.com had an open API, I took it upon myself to write a little Python script that could submit pastes for me.

And so I give you upaste (u for Ubuntu, but it can run on any *nix flavor, including OS X). Please try it out and give me your thoughts. I hope someone will find it useful.

Installing

Download the source from bitbucket.

# Copy the upaste file somewhere in your $PATH
$ sudo cp upaste /usr/local/bin/
 
# Then run!
$ ifconfig | upaste
 
# -h is for help
$ upaste -h
Usage: upaste [options]
  Posts a file to http://pastebin.com/ and returns the URL.
  If no file arguments are given, input is expected on stdin.
 
Options:
  --version           show program's version number and exit
  -h, --help          show this help message and exit
  -p, --private       mark your paste private.
  -f FILENAME         filename whose contents you wish to paste.
  -n PASTE_NAME       add your name or a title to paste.
  -e PASTE_EMAIL      send a confirmation email with paste link.
  -s PASTE_SUBDOMAIN  use a pastebin subdomain.
  -x PASTE_EXPIRE     set paste expiration: N = Never, 10M = 10 Minutes, 1H =
                      1 Hour, 1D = 1 Day (default), 1M = 1 Month.
  -l PASTE_FORMAT     define the default syntax highlighting for your paste.

Vim

So I recently started using Vim as my text-editor of choice. I used to hate Vi with a passion, but lately I’ve grown frustrated with the bloated, laggy IDE’s of today, so I turned to Vim.

Vim is a super lightweight text-editor that you run from the terminal. The command syntax made no sense to me at first, but slowly I started to learn the basics and soon I was working along as fast as I ever had.

The nice thing about Vim is I can ssh into my web-host and edit files right in the terminal without having to muck with a “Save remotely as” feature.

The one thing with Vim is you’ll probably want to enable syntax highlighting right off the bat.

Here’s my ~/.vimrc config file if you’re interested:

syntax on
colorscheme slate
set number
set expandtab
set tabstop=4
set shiftwidth=4

This will turn syntax highlighting and line numbers on, set your default colorscheme to something that isn’t horrible and set your tabs to four spaces (mainly for Python development).

I’m even running split views in Vim now, I feel so 1990’s.