Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Apply url to text in the stylesheet?

    I searched the threads and couldn't find anything that covered this (maybe I missed something...)

    Is there a way to apply a url, a hyperlink, to text in the stylesheet? I'm using the same style for text in various locations on the page, all linking to the same site, but don't want to keep repeating the href.

    Thanks,
    Cbouchar

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    In a stylesheet? Not that I know of. You can use the <base> tag though:

    <base href="http://www.samesite.com/">

    Whenever you want to deviate from this base though you'll have to type out the full URL for your anchor tags. I suppose you could use JavaScript to write out all of the URLs for you as well but it would be a lot of work for little gain unless the URL will be changing frequently.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but let me clarify. What I am trying to do is reduce the occurances of href in my code. Using the <base> element, I still need to use href for my individual text links.
    I know this doesn't work, but what I am looking for would be similar to:

    A.google { COLOR: black; url: www.google.com}

    which would allow me to then have all google-class text be linked to www.google.com, without me entering href each time.
    I invented the code, but does the concept make sense? Is there a way to do something like this with any coding?

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, CSS can not do that. CSS is used to style X/HTML documents. Something that makes an element be a link is not a style, therefore, it is beyond CSS's scope.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you could do it with javascript quite easily:
    (this uses the prototype library, so either grab that or convert it...)
    Code:
    links=$A(document.getElementsByTagName('a'));
    links.each(function(node) {
        node.href="http://www.google.co.uk";
    });
    You could expand this and use a hash of classname=>url pairs.

    I would never use it though, as it won't degrade, people using screen-readers won't know what the links are, and it's really really lazy.

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks GJay
    I'm a bit of a newb, especially when it comes to javascript. How would I impliment the above code?

  • #7
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,785
    Thanks
    8
    Thanked 131 Times in 129 Posts
    a non javascript way could be used with php. you'll have to include the fuction in everypage manually or through an "include" statement. This example changes all the instances of the word "qqq" with "http://www.google.com". I chose "qqq" as the odds of you ever writing "qqq" anywhere else on purpose is slim to none.

    Code:
    <?php
    
    function callback($buffer)
    {
      // replace all the qqq with href
      return (str_replace("qqq", "http://www.google.com", $buffer));
    }
    
    ob_start("callback");
    
    ?>
    <html>
    <body>
    <p><a href="qqq">google</a></p>
    </body>
    </html>
    <?php
    
    ob_end_flush();
    
    ?>
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

  • #8
    New Coder
    Join Date
    Mar 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by harbingerOTV
    a non javascript way could be used with php. you'll have to include the fuction in everypage manually or through an "include" statement. This example changes all the instances of the word "qqq" with "http://www.google.com". I chose "qqq" as the odds of you ever writing "qqq" anywhere else on purpose is slim to none.
    Ah, but remember my objective. I'm trying to reduce the occurances of href, not the url. I'm guessing that I wouldn't be able to put <a qqq> where qqq= href="www.google.com"

    I don't mind using Javascript, if someone could tell me how to impliment it in my page.

  • #9
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Is reducing the instances of the href attribute really worth doing?
    How many times do you need to code that markup?
    You're already hard-coding the anchor tags, so is finishing the job properly with the hrefs really going to be that much extra bother?

    If you really feel the need to avoid repeating the code manually, you could bring the link in as a re-usable include.

    e.g. instead of hard-coding the link in each page, either bring it in as an include or echo it as a variable which you've preset elsewhere in your document.

    linkdefs.php
    Code:
    <?php
    
    $google = '<a href="http://www.google.com/">Google</a>';
    $yahoo = '<a href="http://www.yahoo.com/">Yahoo</a>';
    $msn = '<a href="http://www.msn.com/">MSN</a>';
    
    ?>
    In your markup
    Code:
    <?php include('linkdefs.php') ?>
    ...
    <li><a href="alink.php">A Link</a></li>
    <li><?php echo $google ?></li>
    <li><?php echo $yahoo ?></li>
    <li><?php echo $msn ?></li>
    <li><a href="anotherlink.php">Another Link</a></li>
    ...
    output
    Code:
    ...
    <li><a href="alink.html">A Link</a></li>
    <li><a href="http://www.google.com/">Google</a></li>
    <li><a href="http://www.yahoo.com/">Yahoo</a></li>
    <li><a href="http://www.msn.com/">MSN</a></li>
    <li><a href="anotherlink.php">Another Link</a></li>
    ...
    Of course, you're still going to have to hard-code the php passage (w/ include), but you'll be able to update the link in one file and have it propogate throughout every instance of the include.
    In this sense, it's no different to including a nav menu, which also appears with identical code across a number of pages on a site.

    I'd certainly avoid using javascript for this sort of thing as it's likely to mean having a nasty piece of code in your markup which the js will transform into a recognisable and usable link (though, only for those with js. non-js users will be left with a badly munged link.

    Do not compromise on the quality of the resulting markup.
    Last edited by Bill Posters; 04-23-2006 at 05:26 PM.

  • #10
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,785
    Thanks
    8
    Thanked 131 Times in 129 Posts
    sure you can. now anytime you write "qqq" in your page it will be replaced with a link to Google.

    Code:
    <?php
    
    function callback($buffer)
    {
      // replace all the qqq with href
      return (str_replace("qqq", "<a href='http://www.google.com'>Google</a>", 
    
    $buffer));
    }
    
    ob_start("callback");
    
    ?>
    <html>
    <body>
    <p>qqq whole bunch of other text. and another link to qqq here.</p>
    </body>
    </html>
    <?php
    
    ob_end_flush();
    
    ?>
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis


  •  

    Posting Permissions

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