Possible to detect which stylesheet in use?

This is a discussion on "Possible to detect which stylesheet in use?" within the JavaScript Forum section. This forum, and the thread "Possible to detect which stylesheet in use? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 22nd, 2006, 23:22
New Member
Join Date: Nov 2006
Location: MD
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Possible to detect which stylesheet in use?

Is it possible to use Javascript to detect which stylesheet is currently being used in a page?

... a script to detect the HREF value in the <link> tag which is located in the <HEAD> of the page.

IMPORTANT:
I need to place this javascript "detect" after the <BODY> tag! (= cannot place this script in the <HEAD>)

EXAMPLE:
Code: Select all
<html>
<head>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
blah, blah....
<script> detectCurrentStylesheetBeingUsed() </script>
</body>
</html>
My guess is that this cannot be done ... but I'd like to hear from others.


Thanks.
Reply With Quote

  #2 (permalink)  
Old Nov 23rd, 2006, 03:16
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

Code: Select all
<link rel="stylesheet" type="text/css" href="layout.css" id="style" />

alert(document.getElementById("style").getAttribute("href"));
Reply With Quote
  #3 (permalink)  
Old Nov 23rd, 2006, 03:57
New Member
Join Date: Nov 2006
Location: MD
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

Quote:
Code: Select all
<link rel="stylesheet" type="text/css" href="layout.css" id="style" />

alert(document.getElementById("style").getAttribute("href"));
Unfortunately, I am working on a web page that I am being "fed" that I have no control over. The <LINK> tag does not have an "id" and I cannot add an "id" to the <LINK> tag.

I am only able to manipulate/edit the page within the <BODY> section.

So... I need to DETECT which Stylesheet is specifically being used within the <LINK> tag without an "id".

There are a few different stylesheets that are being used. I need to know which stylesheet is the CURRENT one so that I can counter-code (if that's a real word ) specific font colors, background colors, etc.

If you need futher explanation, I'll be happy to give deeper descriptions. But I think that I've covered the situation.


Thanks.
Reply With Quote
  #4 (permalink)  
Old Nov 23rd, 2006, 09:56
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

Well, if there is no way to directly identify the current stylesheet, there's obviously no way JavaScript can do it...
Reply With Quote
  #5 (permalink)  
Old Nov 23rd, 2006, 09:57
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

Er, I should've asked this before, why can't you edit the head tags? That seems rather silly to me.
Reply With Quote
  #6 (permalink)  
Old Nov 24th, 2006, 03:00
New Member
Join Date: Nov 2006
Location: MD
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

1. I am being "fed" a wrapper for the page. Then I put my content inside the <BODY> of the page.

2. I can see the stylesheet (i.e.: main.css, etc.) that I'm being "fed" in the <HEAD> in the HTML SOURCE, but I can't change the stylesheet.
Reply With Quote
  #7 (permalink)  
Old Nov 24th, 2006, 22:47
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Possible to detect which stylesheet in use?

You can use JavaScript to pick up the link tag(s) and look at its/their attirbute values.

Use the getElementByTagName('tagname'); function.

This actually returns a zero indexed array.
Reply With Quote
  #8 (permalink)  
Old Nov 25th, 2006, 00:49
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

Yeah, but if there are more than one link tags, there is no way to identify which one is the active stylesheet.
Reply With Quote
  #9 (permalink)  
Old Nov 25th, 2006, 05:21
New Member
Join Date: Nov 2006
Location: MD
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

I agree... luckily, the particular site that I'm working with only has one <LINK> tag.

So... here is the script that I was looking for:
Code: Select all
<script>
  hr = document.getElementsByTagName("link")[0].getAttribute('href');
  alert( hr );
</script>
BUT... if there were more than one link, I could use a FOR LOOP to find all of the <LINK> tags. As long as I know what CSS sheet that I'm searching for, I can "grab" that data and do whatever with it.

Like so...
(*Although I haven't tested the specific code below - this is the idea behind what I'm trying say - so no need to write back to tell me if this script works or not ... unless you feel like posting a correct functional version for others.)
Code: Select all
<script>
links = document.getElementsByTagName('link');
for (i=0; i<links.length; i++) {
  if (links[i].rel == "stylesheet") {
     alert(links[i].href)
  }
}
</script>

Thanks to all for the help.
Reply With Quote
  #10 (permalink)  
Old Nov 25th, 2006, 11:43
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Possible to detect which stylesheet in use?

Don't forget you can have more than one rel="stylesheet" on a site. I use three on most of my pages... If it's going to be consistant, then you're set!
Reply With Quote
Reply

Tags
javascript detect stylesheet

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Popup Name Detect RobinDeanDotCom JavaScript Forum 0 Feb 24th, 2007 22:35
Script to detect flash? fezunit JavaScript Forum 5 Jan 28th, 2007 12:18
Detect IE Totti JavaScript Forum 4 Dec 6th, 2006 00:48
Detect whether connection to external url is possible simonneaves Classic ASP 3 Oct 24th, 2005 13:12
detect pop-up blocker - different bert JavaScript Forum 0 Jul 22nd, 2005 10:30


All times are GMT. The time now is 22:22.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43