Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a first time user of jQuery (and a huge fan already). I am trying to make the comic picture of an xkcd page onclick to the explanation page.

Here's what I've done.

  return"" +       

Is this efficient? Is this robust? Any jQuerying tips?

share|improve this question
It won't work on the latest comic ( – Niet the Dark Absol May 19 '14 at 8:31
@Niet Indeed it does. – Simon Kuang May 19 '14 at 16:37
up vote 8 down vote accepted

Besides not needing to return the result of, it looks pretty good. See this thread for an explanation of what returning a value from event does. From this answer,

return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed jQuery.Event object.

e.preventDefault() will prevent the default event from occuring, e.stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up.

Clearly, thats not what we want, so instead of returning the result of, we should just omit the return.

This may not be necessary, but I would be more comfortable using

var comicID = location.pathname.split("/")[1];


var comicID = document.URL.split("/")[3]);

mainly because http:// may be omitted from the URL and the code will still work (shouldn't ever be a problem either way). It's also more intuitive. I would also leave a comment for the code such as

// where path name looks like /156/ for

Indentation would also help the code be more readable, however it's short so it doesn't matter too much... That said, I would write your code:

// when a user clicks the comic open the explanation
   // where pathname is the comicID eg /156/ for"" + location.pathname.split("/")[1]);
share|improve this answer
you should explain what the link says about returning a value from an event just in case the link should die then this question will still be full of the knowledge that you have given the OP – Malachi May 19 '14 at 4:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.