Writing code for a website is quick compared to other programming languages. The problem, though, is because what you build can look different on so many different browsers and platforms it can be huge pain when you are making many small changes to see what it looks like on screen. This gets even more frustrating when you have to test on mobile devices. All you fancy media query people know what I’m talkin’ about. Let’s look at a regular workflow:
- Put your files on a web server (either remotely or on your computer)
- Load up the page in a browser
- See it isn’t quite right
- Make a change
- Hit save, switch to your browser, hit reload.
- Not quite right. Repeat steps 4 and 5 about 100 times.
There’s a better way my friends, and it comes in the form of two different tools working together. The first is LiveReload.
Auto-refresh when you save
LiveReload, as you’ve probably guessed, reloads the page for you as soon as you hit save. This may not sound impressive at first, but ever since the first moment I hit save on a project and saw my changes pop-up in the browser, I was in love. Did I mention it will refresh your mobile devices simultaneously as well?
If you like working with LESS CSS files, it has a built-in LESS support which will compile your LESS files into CSS for you. Any CSS changes are applied to the page without reloading at all. I’m not sure how it accomplishes it, but my best guess is witchcraft. If you’re unfamiliar with LESS, it’s worth checking out.
Before you Begin:
Make sure you have a web server running on your computer. Luckily, every Mac has one built-in. Just go to System Preferences > Sharing. From there make sure that the Web Sharing checkbox is checked. ( For more information check out this article).
- Download the application here (There is a Windows version as well which is still in beta)
- Launch LiveReload
- Click the plus button, and add the folder you want it to monitor for you.
- Paste this code into the HEAD tag of your html you are debugging:
<script> document.write('<script src="http://' + (location.host || 'localhost').split(':') + ':35729/livereload.js?snipver=1"></' + 'script>') </script>
- Browse to the URL of your project in your browser on the computer or mobile ( i.e. http://192.168.1.70/~username/testapp ).
- Save something in your project folder and watch it reload by itself!
Weinre picks up where LiveReload left off. This gives you the Chrome web inspector on any remote device, including your iPhone, iPad, Android. It looks and works exactly like the Chrome development tools do.
- Download the application here (Choose the Mac application if you’re on a Mac.)
- Unzip and copy the Application to your Applications folder.
- Open up the Terminal (under Applications/Utilities/Terminal). We’re going to need to tell Weinre that it is okay to connect to remote devices.
- Type this and then hit return:
- Copy and paste this into the terminal, then hit return:
echo 'boundHost: -all- httpPort: 8081 reuseAddr: true readTimeout: 1 deathTimeout: 5 ' > ~/.weinre/server.properties
- Launch Weinre
- Paste this code into the HEAD tag of your html you are debugging. Make sure to replace YOUR_HOST with your computer’s IP address or host name (i.e. 192.168.1.101 or kipp.local):
- Browse to the URL of your project in your browser on your mobile phone or iPad ( i.e. http://192.168.1.70/~username/testapp )
- You should be able to go back to Weinre and click the Element or Console tab and see live output from your device.
Changing CSS properties and HTML in the Elements tab will be visible live on your device, making it easy to fine-tune positioning of elements, or strange layout glitches. If you are using media queries, this comes in very handy.
An of example of how I used LiveReload and Weinre in my development workflow I mentioned earlier was to look at event properties. This was a life-saver because reliable documentation on the web can be hard to come by.
After setting up a button and hooking it up to a touch event, I typed “console.log(event)” and I could see all the goodies the mobile browser gave me. Turns out Mobile Safari reports every touch point on the screen.
Another way that I used this setup was to tweak the on-screen buttons for iPad (which don’t show up on the desktop version of my site). Instead of making a change to my CSS, saving, reloading, etc, I nudged the numbers in the Inspector until the button appeared where it was supposed to. Then I just used that number and put it back into the CSS to make the change permanent.
That essentially covers the workflow I use for developing both desktop and mobile sites. It allowed me to watch my changes take place on multiple devices at once as soon as I hit save, as well as more easily fine tuning my page. It has been such a lifesaver for me, I hope it is as helpful to you as well.