MShack
MShack

Reputation: 652

How can i add a class to the body depending on url

I need to add a tag to the existing id , the current HTML is

<div id="home" class="pagebody"></div>

and i need to add an additional class to the pagebody ONLY if the url has "MODULE=" in it.

So when the url contains "MODULE=" , the appended html will look like this

<div id="home" class="pagebody hpmessage"></div>

I don't have access to the HTML to manually change this , so i have to use JavaScript / jquery to add the additional class and it must only be added to those urls containing "MODULE=" , any ideas ?

examples of some of the vast URLs are:

http://mysite/2014/home/20832
http://mysite/2014/home/20832/standings
http://mysite/2014/home/20832?MODULE=MESSAGE
http://mysite/2014/home/20832?MODULE=MESSAGE1
http://mysite/2014/home/20832?MODULE=MESSAGE2

Upvotes: 0

Views: 48

Answers (2)

Shomz
Shomz

Reputation: 37701

Vanilla JS version:

if ( window.location.search.indexOf( 'MODULE=' ) > -1 )
    document.body.classList.add( 'hpmessage' );

Upvotes: 0

adeneo
adeneo

Reputation: 318182

if ( window.location.href.indexOf('MODULE=') != -1) {
    $('#home').addClass('hpmessage')
}

Get the current location, check if it contains MODULE-, and if so add a class ?

Upvotes: 4

Related Questions