Posts tagged ‘tips’


Reading GET variables with JavaScript

One of the things that isn’t immediately obvious in JavaScript is how to access GET variables. I’ve seen lots of different implementations for this around the web, but the majority of them are bulkier than they need to be. Here’s my favorite way to do it:

<script type="text/javascript">
    function $_GET(q,s) {
        s = s ? s : window.location.search;
        var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
        return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
    }
</script>

What this gives you is a JavaScript implementation of PHP’s $_GET functionality.  I use a regular expression to keep the code to a minimum. Here is a simple example of how to use it:

// this code would print "hello world" if it was at http://localhost/index.php?var1=hello&var2=world
var var1 = $_GET('var1');
var var2 = $_GET('var2');
document.write(var1 + " " + var2);

Another thing I like about this implementation is that it makes it easy to parse GET variables from arbitrary search strings (ex “?var1=hello&var2=world”).  This is handy if you need to access GET variables from an HTML src parameter such as an image or script tag.

// get the src parameter and split it down to the search query string
var src = document.getElementById('example').src;
params = src.split('?');
var var1 = $_GET('var1','?'+params[1]);
Updated 01/14/11 with Kip Robinson’s improvements from the comments
 34 comments

Looking for a job? Don’t be this guy.

I just received this email:

Hi,

Please see my attached resume.
I’m very intelligent and creative. I have a very eclectic arsenals of skills for the solution of problems.
I’ve worked in numerous startups, including several of my own.

Reed

The sad thing is, I get an email like that just about every day. I thought I would share my response in hopes that it will help someone from making the same mistakes.

We’re not hiring right now, but here are a few free tips:

  • “I’m very intelligent and creative.” doesn’t come off as confident, it comes off as cocky
  • If you had spent 2 minutes looking at our site you would have known that my email address is josh@eventvue.com not careers, not jobs… just josh.
  • No mention about what excites you about EventVue? Keep in mind I get several resumes in my inbox EVERY DAY. It’s not hard to get my attention. Comment on my blog. Send me an engaging question. @me on twitter. I’ll respond. Just don’t send me something that has been copied and pasted to a dozen different companies.
  • “FW: about me” is your subject line? I’d work on that one a bit.
  • We’re a startup trying to build cutting edge stuff. The fact that you sent me an email from a Hotmail account communicates that you aren’t much of an early adopter. That’s too bad, because I bet you’re a smart guy.

I understand that startups are different.  Your career center probably didn’t tell you this stuff.  That’s why I am.

Update. Reed responded:

Don’t worry I’m very creative and intelligent. It’s not a boast. It’s who I am.

If you read my resume then you know that I’m also an internationally known composer.
I can write top level music in any style you can think of, including the most modern remix and such.
I attached a song from one of my Cds. All my Cds have been in or near the 10 ten the country on jazz radio.

I  have not only hotmail but gmail and facebook and twitter and others.

Anyway, I’ll keep your advice in mind.

I’ll check out your blog.

Not sure that helped, but at least now I know he’s a good composer.  The music was pretty.

 16 comments