jQuery Flow Control (if then from URL params)

Posted by Ryan Max on Stack Overflow See other posts from Stack Overflow or by Ryan Max
Published on 2010-04-23T05:37:44Z Indexed on 2010/04/23 5:43 UTC
Read the original article Hit count: 479

Filed under:
|
|
|
|

Strangely enough I am more familiar with jQuery than I am with javascript. I need to be able to add a class to the body tag of a document depending on what specific forum page i'm on in a phpbb forum. Due to the nature of phpbb I can't actually do this flow control in php, so I am using jquery.

Here's my code (the first part is an extend that gets the url parameters like so

http://www.mysite.com/viewforum.php?f=3

var forum = $.getUrlVar('f');

will make forum == 3

because of the nature of phpbb i can't really do any flow control with php. So I am using jquery. This is my code:

$(document).ready(function(){

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
});

$(document).ready(function(){
var forum = $.getUrlVar('f');
if (forum == 3){ $('body').toggleClass('black'); }
});

Yet this isn't working. Any idea why not?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about phpbb