Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
If the URL contains something... do this
Old 06-26-2009, 07:49 AM If the URL contains something... do this
tuestunim's Avatar
Experienced Talker

Posts: 41
Name: Scott Brown
Location: Huddersfield, UK
Trades: 0
Hi,

I have a page where I cannot edit the html directly so am doing a lot of chopping and changing with jQuery.

What I need to happen is for javascript/jquery to apply a class to the body tag if the url contains something. For example:

if the URL contains "best-sellers", I want the body tag to be <body class="best-sellers">.

I've used jqURL which allowed me to do what I wanted but at massively bloated file sizes as I had to define the whole url for each rule. Also, each page where I want this to be applied can be accessed on 4 (slightly) different addresses. The common theme between these is the text 'best-sellers' which is why I want to target that.

I've tried probably 20-30 different ways of marking this up, changing syntax, using jQuery, not using jQuery and so on. If someone's got any idea how to go about this I'd be most grateful if they told me!

Cheers guys,

Scott.
tuestunim is offline
Reply With Quote
View Public Profile Visit tuestunim's homepage!
 
 
Register now for full access!
Old 06-26-2009, 10:02 AM Re: If the URL contains something... do this
tuestunim's Avatar
Experienced Talker

Posts: 41
Name: Scott Brown
Location: Huddersfield, UK
Trades: 0
I think I've gone and done it myself you know, despite the avatar I use.

Code:
$(document).ready(function() {

  var url = location.pathname;

  if (url.indexOf("best-sellers")) {
    $("body").attr("class","pm-best-sellers");
  }
});
That's seeming to do the job at the moment.

If anyone has a tidier version, spots any mistakes or, if there aren't any, just wants to congratulate someone who's managed to make a piece of javascript work despite being turd at it, let me know.

Ta,

Scott.
tuestunim is offline
Reply With Quote
View Public Profile Visit tuestunim's homepage!
 
Old 06-26-2009, 10:52 AM Re: If the URL contains something... do this
tuestunim's Avatar
Experienced Talker

Posts: 41
Name: Scott Brown
Location: Huddersfield, UK
Trades: 0
As with everything in javascript I try, I seem to get so far then fall over. Please take a look at this:

Code:
var url = location.pathname;

$(document).ready(function() {
  if (url.indexOf("best-sellers")) {
    $("body").attr("class","pm-best-sellers");
  }
  
  else if (url.indexOf("express-gifts")) {
    $("body").attr("class","pm-express-gifts");
  }
  
  else if (url.indexOf("eco-friendly")) {
    $("body").attr("class","eco-friendly");
  }
  
});
The bit of code in the last post seems to execute on every page, making the class "pm-best-sellers". Obviously I only want it to happen on pages with "best-sellers" in the url.

On top of that, I want to assign three of this type of rule depending on the url.

I'm sure you can see what I'm trying to do here. Can anyone help me out? I promise to study and understand it!

Thanks,

Scott.
tuestunim is offline
Reply With Quote
View Public Profile Visit tuestunim's homepage!
 
Reply     « Reply to If the URL contains something... do this
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11608 seconds with 12 queries