Creative ways to solve common problems – II

The Problem: The Javascript XHttpRequests (aka AJAX) has cross domain restrictions that keeps you from making POST requests to a different domain from the one running your client code.

The Solution: One of the most used ones is to create an iFrame with the POST data and add it to the document and submit it. But you’ll get no response back from it (like a success or failure message).

The Creative Solution: I came across this one in here and basically it relies on the fact that images are not subject to cross domain restrictions, so basically you’ll load an “image” from the server that will return a different sized image per possible result. So maybe you’ll have the possible results as SUCCESS and FAILED. You’ll just have to return let’s say a 1px image for SUCESS and a 2px image for FAILED. Then you’ll just have to check the size of the image to “read” the return message.

Safari Mobile breaks jquery.live()

The live() method of jQuery is a blessing for dynamic interfaces. It allows you to attach events to dynamically created elements.

Say you want to create a dynamic menu based on some live data read from a server. Maybe you want to create some HTML like this:

<nav>
<ul>
	<li id="nav1" class="btn">item1</li>
	<li id="nav2" class="btn">item1</li>
	<li id="nav3" class="btn">item1</li>
	<li id="nav4" class="btn">item1</li>
</ul>
</nav>

With jQuery you can dynamically generate clickable elements like this:

$(".btn").live("click", function(){
var elem = event.target.id;
alert("you clicked: " + elem);
});

This works well in the desktop and in every browser but on Mobile Safari it fails.

A workaround is to deliberately add an “onclick” attribute to the element you want to attach the event:

<ul>
	<li id="nav1" class="btn" onclick="">item1</li>
</ul>

How to install Git support on a Bluehost (or Hostmonster) shared account

I had a couple of sites hosted on a Bluehost shared account and I’ve recently moved from using SVN (Subversion) to Git (I’ll try to post soon a beginner’s guide to Git), and I really wanted a fast and feasible solution for the deployment of those sites updates (really a better way than to FTP the changes, believe me).

Bluehost doesn’t support Git “out of the box”, but if you have SSH access to your account (if you don’t, just open a support ticket and ask for it. Probably you’ll need to fax or e-mail them your ID card) you can install it yourself.

1. Log into your Bluehost account via the terminal with SSH: ssh youruser@yourdomain.com (you’ll be asked for the password) and you’ll get into your home dir:

Last login: Mon NovĀ  2 08:34:58 2009 from 253.116.54.71.rev.vodafone.pt
user@domain.pt [~]#

2. Now we need to download the Git software:

user@domain.pt [~]# wget http://kernel.org/pub/software/scm/git/git-1.6.5.2.tar.bz2

This will download the Git package (compressed as tar.bz2) into the home dir of our account (please note that I’m getting the last Git version available at the time of this post but you’ll want the latest one, so make sure to check the filename first on the Git website.

3. Extract the files from the downloaded package:

user@domain.pt [~]# tar -xjvf git-1.6.5.2.tar.bz2

4. Go into the extracted directory and compile Git:

user@domain.pt [~]# cd git-1.6.5.2
user@domain.pt [~]# make

5. This will take a couple of seconds. Then you’ll be ready to install it:

user@domain.pt [~]# make install

And that’s it! You now have Git installed on your Bluehost account!

Now you can type git and hit return and you’ll get the list of the Git commands:
git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
[-p|--paginate|--no-pager]
[--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
[--help] COMMAND [ARGS]

The most commonly used git commands are:
add Add file contents to the index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
fetch Download objects and refs from another repository
grep Print lines matching a pattern
init Create an empty git repository or reinitialize an existing one
log Show commit logs
merge Join two or more development histories together
mv Move or rename a file, a directory, or a symlink
pull Fetch from and merge with another repository or a local branch
push Update remote refs along with associated objects
rebase Forward-port local commits to the updated upstream head
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status
tag Create, list, delete or verify a tag object signed with GPG

See 'git help COMMAND' for more information on a specific command.
user@domain.pt [~]#