Learning to write JavaScript

So now that I work at Mozilla, I figured it was time to develop a “web app” just to make sure I understood it all. And since my team is working on educational resources for web developers, I wanted to see what it was like to learn how to use some of them using resources online.

So I decided to use the resources I could find online and write some JavaScript to do a pet project of mine.

What was the problem I wanted to solve?

I really wanted a way to automatically add “Photo by <author>” to the bottom of Flickr pages I want to use in my presentations. I have a number of workarounds I’ve developed to do this in an effective way, but I really just wanted to click a button.  For example, at first, I saved the image with the author’s name as the file name, and then every time I used the picture, I added text to the slides. Then I wrote a Ruby script that went through all my picture files and added “Photo by <filename>” to them. But that is still a three step process: grab the author’s name, save the photo as the author’s name and then run the script. Also, I like searching on Flickr better than I like searching my personal archive. Tags are nice.

I also wanted to make it a service and a bookmarklet so others could easily use it.

Why did I decide to use JavaScript?

When I first started this problem, I wrote a script in Ruby. I thought I’d use Ruby on Rails to make it into a web app, but Rails looked pretty difficult to setup for a newbie, so I put it on the back burner.

Now that I’m at Mozilla, I thought it was high time to learn JavaScript.

(For the record, I have a background in C, C++ and Java. I find Ruby much more intuitive to read than JavaScript. But I find JavaScript much more readable than many other things.)

In retrospect, the Flickr API is difficult to use from JavaScript. I think using PHP would have been much easier. It did force me to use only one call to Flickr (the right solution), as adding a Flickr API call to JavaScript turned out  to be unintuitive to me. You basically build a <script> object and then append it to the document.

Like this (copied from sample code):

document.flickrURL = ‘http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=’ + document.apikey + ‘&photo_id=’ + photoNumber + ‘&format=json’;

//add the flickr javascript to the page so it gets executed
//flickr automatically calls jsonflickrAPI(rsp)
var root = document.getElementsByTagName(‘head’)[0];
var oS = document.createElement(‘script’);
oS.setAttribute(‘type’, ‘text/javascript’);
oS.setAttribute(‘src’, document.flickrURL);
root.appendChild(oS);

Maybe that’s the way things are usually done in JavaScript, but it took a while to wrap my head around it. I felt like I was dynamically changing the code at execution time which made me feel like I was in a science fiction movie. Except that I didn’t think my problem warranted that complicated of a solution.

Part of my comprehension problem was that a call to Flickr calls your jsonFlickrAPI() function and you don’t get to say when that’s called. Nor change it for different calls. (Although maybe you could have several JavaScript files each with their own jsonFlickrAPI() functions. I didn’t try that.)

How did I learn?

I asked one really stupid question of my team mates. Then I decided I really needed to see if I could learn this from web resources.

I used primarily three:

  1. MDN. When I wanted to learn more about an element or what was possible, I ended up on MDN. Probably because I was familiar with this site – it’s the one that my team at Mozilla maintains! But I really didn’t find any other site that covered all topics like a reference guide. It did turn up several times in my searches too.
  2. StackOverflow. I googled most of the problems I had and several time I found good answers on StackOverflow.
  3. Personal blogs. A lot of my questions and answers were found on personal blogs. People had encountered a similar problem and they blogged about it.

How’d I get started?

It’s been a long time since I’ve written code. (Other than an occasional program or script to solve a quick problem.) I broke my problem down into nice little steps. Each one of which was functional on its own.

For example:

  1. First I wrote a “Hello World” app in JavaScript. This was to make sure I knew where the code should go, the basics of getting setup with an editor, etc. (Very easy.)
  2. Then I figured out how to make a canvas and draw an image on it. (Easy.)
  3. Then how to write text on an image. (Easy.)
  4. Then how to create a jpg of that canvas. (Was easy. Then it quit working.)
  5. Then I figured out how to have the user specify the image location and the text. (Pretty easy.)
  6. Then I figured out how to get an image from Flickr. (Not easy.)
  7. Then I figured out how to get an image from Flickr when all you have is the url of the photo page. (Really not easy.)
  8. Then I figured out how to put some javascript on a website for everyone to use. (Very easy.)
  9. Then I created a bookmarklet. (Not as easy as I thought but quick.)

What was hard?

Trouble shooting and Flickr.

  • Trouble shooting JavaScript was not always easy. If I was getting someone started with JavaScript, I’d set up their development environment and explain the tools first. Firebug, the Firefox Console and alerts ended up being my friends. Before I do more JavaScript development, I’ll explore some more debugging tools.
  • Flickr. I think the Flickr API might be really easy to use if you use a language where you could just make a call from the code. I had to create a URL and then append it as a script. It made trouble shooting harder and it felt clumsy.

A couple of times I also ran into something that made no sense to me or was taking too long to trouble shoot and instead of figuring it out, I did it a different way. I think it would be good for my education to figure out what was wrong with the initial approaches.

What did I end up with?


If you’d like to try it out, drag this bookmarklet [thisphotoby] to your bookmarks bar. (If you are on Internet Explorer, right click and save it.) Then go to a Flickr photo page and click on the bookmarklet. It will return the photo with “Photo by <author>, <url>” overlayed over the  bottom of the photo.

You can also check out the (very ugly) website I made, thisphotoby.com.

Next steps

There are a lot of things I could do to improve this.

  1. Add error handling. If anything goes wrong, well, it just doesn’t work. I didn’t do any checking or give the user any helpful tips. A terrible coding practice!
  2. Clean up the code. I copied and pasted ideas from many places and ended up with variable names and function names that follow no standard. I’d also like to find a JavaScript style guide and clean up the code.
  3. Add user preferences. It would be nice to specify which size image you want, if you want white or black text, what size text, etc.
  4. Licenses. I’d like to be able to check what permissions the user has and what license the photo is under in order to help the user understand how they can use the photo. Flickr lets you download all sizes of all photos even if they are licensed “All Rights Reserved”. This script passes on that ability, but it would be nice to also make the licensing more obvious.
  5. Create a jpg. I had it working with a png and a jpg. Then the jpg part quit working. I tried several other methods and did not get it working. Since I felt like I had accomplished my goal, I left this for later.
  6. Make a pretty website. 🙂

Help girls discover how cool a career in technology could be!

If you have some time this Saturday, take half an hour or so to help show girls how cool a career in technology can be. Dare 2B Digital is a conference for girls to learn more about careers involving computers.

What can you do?

As part of the conference, the girls will be making videos. (Mozilla is sponsoring the event and Lukas Blakk will be teaching the HTML5/Open Video workshop.) We need volunteers to help transcribe the videos and translate them real time to show them the power of the community and technology. The software being used is Universal Subtitles (which is a really cool tool).

Spend half an hour this Saturday and help show girls how cool our world is. You can sign up here.

Why help girls learn about computer science?

As many of you know, less than 2% of open source software developers are women. This frustrates me for two reasons.

  1. A bunch of women are missing out on some really awesome opportunities.
  2. The open source community could be twice as big! Imagine all we’d get done!

But it’s hard to recruit more women when the pool of women in computer science in general is small. I helped out at a technology event for girls once. They were sixth graders (about 12 years old). When we asked what they wanted to be, none of them picked any kind of technology field. Studies show that by sixth grade, girls have already decided not to pursue math and science careers.

This is our chance to show them how cool those fields can be. And that they aren’t alone, that there’s a whole community of interesting and motivated people they’d get to work with.

So sign up now. You can help in person if you are in Mountain View or you can help transcribe and translate if you are not. Bilingual people are much needed!

LinkedIn Maps: How are your friends connected?

LinkedIn Maps takes your LinkedIn contacts and groups them. The result is not only interesting but rather pretty. You can mouse over the different nodes, see who they are and who else is connected to them. After a few nodes I was able to see patterns in all my groups but one …

The group I can’t identify is dispersed throughout the whole map and contains friends from college, old collegues from HP as well as people that used to work at O’Reilly. I believe the group is really one of people that don’t share enough connections with others in my network to fall into any one group.

Too skinny and too fat: what happened to normal?

Photo by Camera baba' aka Udit Kulshrestha

You hear a lot about how the media potrays super-skinny models and they make poor role models for young women.

Today I turned on the TV and there was a cartoon with an obese girl in it. Interesting, I thought, they’re trying to portray reality. Then I noticed that every other character had a waist the size of their arm. (Literally, I paused and checked. The male characters had muscular arms and so had slightly bigger waists. The girls all had thin arms and ridiculously thin waists.) So there were a whole bunch of super skinny characters plus one obese girl and one obese boy.

What kind of body image message is that sending?

Learning division in school these days

One of the things that has frustrated us with our 10 year old’s current school is that he has no text books. This makes it hard for us to look up how he’s learning things. It’s especially frustrating in math as they now learn different methods we did.

Recently, I was struggling to show him how to divide. (Well, I wasn’t struggling to show him how to divide but my way seemed to be completely foreign to him, as if he’d never learned it before. So I was struggling to build on what he already knew.)

So I asked his teacher how he had taught the class to divide, and he sent me back this work sheet with the “4 methods he had taught them”.

Nevermind that those are 3 methods and one explanation on how to write problems – I will admit that at times I’m a bit too pedantic about saying exactly what you mean. (But really, it’s math, you have to say 3 when you mean 3!)

While it seems like a good way to understand what division is all about, it seems to be lacking in ways to easily come up with the exact answer. But it does work to teach them how multiplication and division are related.

I also really liked that it was immediately followed up with word problems, i.e. “Mike has 32 cookies. He wants to share them equally with his 6 friends. How many cookies does each friend get?”

I hope there’s a next step where they learn how to divide the “good old-fashioned way”.  Whether or not they do, I’ve already taught my 10 year old that way, although I had to teach him decimals as well. (I’m sure that when I first learned to divide, they very conveniently left out all problems with remainders and then added them in later.)

Which way do you think is the best way to teach kids about division?

My new Audible books won’t load on my Android phone

Audible now has an Android app, yeah!

But recently, I couldn’t get my new books to show up on my phone. I followed all the steps in their support forums:

  1. Make sure you’re signed in with the right account.
  2. Refresh.
  3. Sign out, sign back in.
  4. Uninstall the app, reinstall.
  5. Uninstall, delete the AudibleActivation.sys file, reinstall.

Still nothing. I emailed support with everything I’d done and they told me to refresh.

So then I uninstalled, deleted the entire Audible directory from my SD card and reinstalled.

Now I can see my new books on my phone.

Just posted in case you are having the same problem. Hopefully Audible will also update their support forums.

Guns, drugs and homosexuals and why I hate discussing politics

At our Christmas dinner, we ended up discussing guns, drugs and homosexuals. Plus some politics.

I hate discussing politics. And I had a hard time with the guns, drugs and homosexual conversations too. Not because I don’t hold opinions. Not because I don’t care about them. Not because I’m afraid to share my opinions. And not because I’m afraid of controversy or loud arguments.

I hate discussing these hard topics because I know I haven’t set myself up to succeed – to convince the other person my view is right. Words and facts and heated discussions rarely change anyone’s mind.

If you really want to change someone’s mind, you have to truly understand why they think they way they do. I can show people how open source software will help them because I understand why they are suspicious of free software. I understand what their concerns and hesitations are. And I don’t just understand them, I can feel their anxiety. I can imagine what they have to explain to their bosses and how they’ll feel responsible. I also know enough about free and open source software to address all their concerns and point out what’s a myth.

I truly don’t understand why people think sending millions of people through scanners at the airport will make us all safer. And I truly don’t understand many people’s positions on homosexuality, religion, guns and drugs.

If I did, perhaps I could convince them my views would lead us to a better world. But until I can truly understand why they feel the way they do, I feel very under-qualified to change anyone’s opinion.

To top it off, I think we also spend too much time avoiding the root of the problem and discussing how to fix the symptoms. For example, take abortion. Nobody wants an abortion. Nobody goes out and gets pregnant because they want to get an abortion. So why do we spend millions of dollars, lots of emotions and tons of time debating whether abortion should be legal or not? Because we’re avoiding the real problem – why people who didn’t want to get pregnant, got pregnant and how we might help people like them avoid that in the future.

I think the same problem (abortion vs getting pregnant) applies to most emotional topics. When discussing guns, drugs, homosexuality, etc, we tend to focus a lot of our energy on things that are symptoms (unwanted pregnancies) rather than on the root cause (helping people avoid getting pregnant if they don’t want to be.)

Oh, and we didn’t discuss abortions at Christmas dinner. Maybe next year.

Is it bad to argue in front of your kids? (Was: apologize to someone you’ve wronged.)

Is it bad to argue in front of your kids?

Benjamin Zander, the author of The Art of Possibility (My review: The book that changed my life the day I read it), has started a meme promoted by Miguel and Jeffrey Stedfast, to apologize to someone you’ve wronged.

My first instinct was to apologize to my kids for arguing in front of them. When we do that it really bothers me and I wonder how it affects them. However, according to NurtureShock by Po Bronson (recommended by Cathy Malmrose), while hearing parents argue stresses kids out, if they hear the end of the argument (and hopefully two happy parents) they go back to feeling normal stress levels. And they learn about conflict resolution. But if you “take the argument upstairs” and they don’t hear the resolution, they remain stressed.

So perhaps I should now have a discussion with Frank about how we argue in front of the kids. I sent them to their rooms and when that didn’t work Frank sent them to watch tv (which was a better distraction but still not enough.)

But if I let the kids watch an argument I have to answer all sorts of very difficult questions. I regularly get asked about why our old car couldn’t be fixed, why owls eat rabbits, why cars need gas, why we can’t have cookies for breakfast, why I wear contacts, why we have to wear clean clothes, why our dog will die one day … and explaining why we were arguing about whatever we were arguing about … well I just want to say “go to your room”! So perhaps the real apology I owe to my kids is for lazily not wanting to explain the argument to them.

But really, there are some subjects you’d rather not get asked why about too much …

Kids on Computers sets up more labs in schools

Sewing at girls' school in Santo Domingo, Oaxaca. Photo by Thomas Peters.

Kids on Computers folks have been busy, especially Hermes and Thomas and others in Oaxaca. In the past few months we’ve set up labs in a school for kids with disabilities and a boarding school for girls – both in small towns in Oaxaca, Mexico.

The lab for the kids with disabilities will also help their parents who will now be able to do research on the web both on their kids’ health and also on government resources that might be available.

Hermes and one of the students. They were all delighted to get to use a computer! Photo belongs to Hermes.

The lab in the girls’ school will enable them to learn skills for a whole new range of jobs. Right now they learn skills like sewing and hair dressing in addition to normal school subjects to learn skills that might help them find a job.

Some of the computers were donated locally. Some by individuals, some by companies. Some, those that are in the girls’ school, came from Partimus. Some came from individuals that donated through System76.

Dormitory at the girls' school. Photo by Thomas Peters.

Thanks to everyone who helped bring technology, open source software and the internet to these kids. I’m looking forward to meeting them online.

The next schools will be in Mexico, Zambia and India.

The terrorists have accomplished their mission

Terrorism is the systematic use of terror especially as a means of coercion. … Common definitions of terrorism refer only to those violent acts which are intended to create fear (terror) (Wikipedia)

Right after 9/11, I flew to Australia for a vacation I had been planning for almost a year. Many people asked me if I was afraid to fly and implied that I should have stayed home, close to family and friends. I replied that if I had stayed home, the terrorists would have won.

Unfortunately, my government does not agree with my definition of winning. They think that living in fear and trying desperately to keep us all 100% safe while flying is the most effective way to fight terrorism. It reminds me of a boss that told me he liked it when people lived in fear of being fired, they worked harder. I told him being fired held no fear for me. When you live in fear, you do irrational things – like sending millions of people’s shoes through an xray scanner every day.

The terrorists that used planes as bombs on September 11, 2001 have changed our lives forever. Now I spend hours each month standing in line waiting to be closely inspected and treated as a potential terrorist myself. I buy shampoo in small bottles. I buy special bags to get though security faster. My life and our economy has been fundamentally changed by those terrorists. Not because it needed to change in response to their actions but because we choose to let them create irrational fear in us. We allowed them to terrorize us.

I had to watch my older son fight back tears at the airport as his bags were taken, all his toys were examined by a stranger and his bag was searched for explosive residue. And I had to answer his questions about why they were doing this and why I was letting them.

Today I read that the TSA will now tell children that groping them is a game. Terrorists, through a series of acts in one day 9 years ago, are now causing our children to be sexually molested when we travel. Having a stranger touch your genitals is not a game unless you are both consenting adults.

We need to grow up, crawl out from underneath the bed, trust each other and fight back. We need to carry our fingernail clippers and our knives on the airplane again. We need to give up the charade that we can be stripped of everything that can be a weapon. We need to fight back with intelligence, not fear. Invest all the money that is going into scanners and use it to fight terrorists not travelers.

Remove TSA from the airport process. Let airlines decide how to run security for their flights and let travelers vote with their money for the type of security they want.

Take the money you were using to fund TSA and fight terrorists. Fight terrorists in a much smarter, more targeted fashion. And while you are at it, think beyond weapons as planes. I certainly think the terrorists are thinking beyond planes at this point. But that tactic sure worked well for them!