Results 1 to 10 of 10

Thread: Stylesheet...

  1. #1
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stylesheet...

    Is there a way to change the style sheet of the page upon a click of a link? Like say, I was testing something and had a black page /w white text, and with a click, change it to a white page with black text?

    I know it can be done with just the plain font alone, but what about the background?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It is much easier to change style properties or even, element attributes. Some browsers allow for choosing from a menu of external styles but, if you want your page's changes to be accessible to the vast majority of javascript enabled browsers. You are better off accessing properties or attributes. Here is a simple scripted form for the body of your page:
    Code:
    <form name="thiscolor">
    <p><input type="text"></p>
    <input type="button" 
    value="Type a background color and click to change to that color" 
    onClick="document.getElementsByTagName? document.getElementsByTagName('body')[0].style.background=unescape(document.forms['thiscolor'][0].value) : document.bgColor=unescape(document.forms['thiscolor'][0].value);">
    </form>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You might want to look at this as I recently asked much the same question:

    http://www.dynamicdrive.com/forums/s...ead.php?t=3459

  4. #4
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Jscheuer1, is there anyway to do this so that there are preset colors?

    AlistairH, that's exactly what I'm looking for, but I don't use PHP on my pages...



    Wait, I saw a place that has the option to change font colors... Here

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Sure:
    Code:
    <input type="button" 
    value="Red" 
    onClick="document.getElementsByTagName? document.getElementsByTagName('body')[0].style.background='red' : document.bgColor='red';">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It works, whoo.

    Finally my last question, hopefully. How can I get that to change font color as well?

    I don't know much, so I'm not sure this is right:

    Code:
    <a href="javascript:void(0)" onClick="document.getElementsByTagName? document.getElementsByTagName('body')[0].style.background='black' : document.bgColor='red';onClick="document.getElementsByTagName? document.getElementsByTagName('body')[0].style.color='white' : document.Color='red'">...</a>
    Last edited by Studly Cannon; 06-27-2005 at 11:15 PM.

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Studly Cannon
    AlistairH, that's exactly what I'm looking for, but I don't use PHP on my pages...
    Any server-side language is suitable, if you have none available at all, then you can use the client-side code that I posted. I just suggested that it's best to use both in tandem as it provides a guaranteed solution that will work for everyone. If you do use the suggestion in the thread Alistair cited, use the first version.

    That said, if you're changing colours interactively, rather than changing the style sheet itself, then it isn't a good solution.


    As for changing body text colours:

    Code:
    function setColours(foreground, background) {
      var body = document.body,
          style;
    
      if(body && (style = body.style)) {
        style.color = foreground;
        style.backgroundColor = background;
      }
    }
    The expected arguments are strings that specify a colour in some CSS-compliant way.

    <a href="javascript:void(0)"
    The javascript: pseudo-scheme should rarely ever be used as it tends to cause problems, such as stopping animation, and being nonsense for script-disabled user agents. If you really cannot provide a link to somewhere useful, then use a pattern like:

    HTML Code:
    <a href="#" onclick="/* ... */; return false;">...</a>
    With regard to the onclick attribute, you can only have one per element (that's true of any attribute) so you must combine the two by ending the first statement with a semicolon. The HTML snippet above shows this to a degree.

    Mike

  8. #8
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Studly Cannon
    AlistairH, that's exactly what I'm looking for, but I don't use PHP on my pages...
    The solution I'm using only uses client-side javascript to load a corresponding style sheet to change colours. I have an example here:

    http://alistairhamilton.homecall.co....ape/index.html

    This references 7 separate style sheets: general.css is a persistent one and contains all the style settings except those related to colour. The others, cityscapeblue.css, cityscapegreen.css, cityscapered.css, cityscapeorange.css, cityscapeteal.css and cityscapepurple.css (all contained in the .../cityscapes/css/ folder) contain, as you've probably guessed, the colour settings.

    I've just started learning to use css positioning to produce my layout instead of tables so though the above pages work in IE, you won't be surprised to learn that FireFox isn't too happy with it ...

  9. #9
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So all I need is the style sheets and it'll work? How do you make the links change them?

  10. #10
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The links in the menu call a javascript function held in the jslibrary.js file which is referenced at the beginning of the html file. The code is that supplied by Mike in the thread I refered to earlier:

    Code:
    function activateStyleSheet(title){
    	var c, i, t;
    	if(!(c = document.styleSheets)) {return;}
    	i = c.length;
    	while(i--){
    		if((t = c[i].title)){
    			c[i].disabled = (title != t);
    		}
    	}
    }
    Have a look at the html code in the webpage to see how the function is called and who the stylesheets are refered to.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •