Reading GET variables with JavaScript

Recently I needed a JavaScript implementation of PHP’s $_GET functionality. There are a lot of quite bulky solutions out there, but my favourite was by Josh Fraser here

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

Short, simple, effective. Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *