By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,036 Members | 978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,036 IT Pros & Developers. It's quick & easy.

Possible to detect which stylesheet in use?

mar2195
P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="main.css" type="text/css">
  4. </head>
  5. <body>
  6. blah, blah....
  7. <script> detectCurrentStylesheetBeingUsed() </script>
  8. </body>
  9. </html>
  10.  
My guess is that this cannot be done ... but I'd like to hear from others.


Thanks.
Nov 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: 26
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="main.css" type="text/css">
  4. </head>
  5. <body>
  6. blah, blah....
  7. <script> detectCurrentStylesheetBeingUsed() </script>
  8. </body>
  9. </html>
  10.  
My guess is that this cannot be done ... but I'd like to hear from others.


Thanks.

Of course, its possible. to detect which stylesheet is being used. Put the following code inside detectCurrentStylesheetBeingUsed().

Expand|Select|Wrap|Line Numbers
  1. function detectCurrentStylesheetBeingUsed(){
  2.     var styleObj;
  3.     styleObj = document.getElementsByTagName("link");
  4.     if(styleObj.getAttribute("rel").indexOf("style") != -1){
  5.         alert(styleObj.href);
  6.     }
  7. }
I hope it works for you.
Nov 23 '06 #2

P: 26
Please ignore my last post. The following code should work. Put the following code inside detectCurrentStylesheetBeingUsed().

Expand|Select|Wrap|Line Numbers
  1. function detectCurrentStylesheetBeingUsed(){
  2.     var styleObj = document.getElementsByTagName("link");
  3.     var i=0;
  4.     while(styleObj[i]){
  5.         if(styleObj[i].getAttribute("rel").indexOf("style") != -1){
  6.             alert(styleObj[i].href);
  7.         }
  8.         i++;
  9.     }
  10. }
This will give you all the stylesheets that your html document is using. [/quote]
Nov 23 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.