0

HELP! lol i cant figure it out!! aaarrrghhhh

heres what i got

HTML:

<html xmlns="http://www.w3.org/1999/xhtml" class="bg1">

Jquery:

$(document).ready(function(){
  var identifier = window.location.pathname;
  switch(identifier)
        {
            case: 'where-to-buy-our-goods.php';
            $('html').removeClass('bg1').addClass('bg2');
            break;
            case: 'about.php';
            $('html').removeClass('bg1').addClass('bg3');
            break;
            case: 'press.php';
            $('html').removeClass('bg1').addClass('bg4');
            break;
            case: 'contact.php';
            $('html').removeClass('bg1').addClass('bg5');
            break;
        }

});

also i think this might have something to do with it...

my site is in a folder on the root as a tester...

so the url is www.URL.com/Folder/about.php for exapmle... idk if this changes what the window.location.pathname should look like...

please help

8
  • 4
    Is there a reason you're not adding these classes directly to the page that you're interested in? For example, on index.php, adding a script that sets your class to bg1. Commented Feb 15, 2011 at 21:22
  • the header with the html and body tags are php includes so it needs to be more dynamic i think Commented Feb 15, 2011 at 21:24
  • hmmm but i guess there would be like a onload that would change my class that i could put into the body Commented Feb 15, 2011 at 21:25
  • 1
    how is php not dynamic? Just because they're in include files doesn't mean you can't figure out what page is being requested and add the appropriate class. Commented Feb 15, 2011 at 21:28
  • uh yeah i didnt say php wasn't dynamic. but i cant program i know you can case switch with php, i just dont know how. if you know how please let me know, i would rather use php than jquery Commented Feb 15, 2011 at 21:40

4 Answers 4

1

case "index.php":

NOT

case: "index.php;

Sign up to request clarification or add additional context in comments.

Comments

0

In order to decide off the path of the url bar:

$(document).ready(function(){
  var identifier = window.location.pathname;
  switch(identifier)
        {
            case: 'index.php';
            //edit proper tag's css
            $('body').css('background','img.jpg');
            break;
            case: 'about.php';
            //edit proper tag's css
            $('body').css('background','img2.jpg');
            break;
        }

});

Substitute body for the proper tag.

Perhaps though, there are better alternatives to this... couldn't you have different body classes to change the background image depending on what page you are on?

2 Comments

yeah i have it in classes... in my question i asked which would be better.... classes or css bg change... ima use classes
CSS would definitely be more intuitive and more efficient. You could have each page change the body tag's class, and then all the other styles could inherit from it. You could even have all the descendant styles in the page adjust based off the body's class.
0

Get URL:

Get current URL in JavaScript?

You can then use the switch/case statement:

http://javascript.about.com/library/bltut05.htm

Comments

0

Lets suppose you wanted to add the background to the body. I will first suggest 2 CSS ways, then if none work for you, I'll suggest a JavaScript way.

<style type="text/css">
   body{ background:url(bg1.jpg) };
</style>

And you would change bg1.jpg in each HTML file.

<body style="background:url(bg1.jpg)">

Same for this one. And JavaScript (jQuery):

$(document).ready(function(){
   switch(window.location.pathname){
      case "index.php":
        $("body").css("background","url(bg1.jpg)");
      break;
   }
});

And you would add how many cases you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.