...

View Full Version : Apply url to text in the stylesheet?



cbouchar
04-23-2006, 01:59 AM
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

Arbitrator
04-23-2006, 04:11 AM
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.

cbouchar
04-23-2006, 06:16 AM
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?

Kravvitz
04-23-2006, 06:20 AM
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.

GJay
04-23-2006, 10:34 AM
you could do it with javascript quite easily:
(this uses the prototype library, so either grab that or convert it...)


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.

cbouchar
04-23-2006, 05:11 PM
Thanks GJay
I'm a bit of a newb, especially when it comes to javascript. How would I impliment the above code?

harbingerOTV
04-23-2006, 05:29 PM
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.



<?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();

?>

cbouchar
04-23-2006, 05:42 PM
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.

Bill Posters
04-23-2006, 06:19 PM
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

<?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

<?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

...
<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.

harbingerOTV
04-23-2006, 06:29 PM
sure you can. now anytime you write "qqq" in your page it will be replaced with a link to Google.



<?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();

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum