Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

The thing is I have a table containing ~2000 rows where each row contain a link that has the same title e.g.

<a id="1" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere1">My 1st link</a>
[...]    
<a id="2000" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere2000">My 2000th link</a>

I'm wondering if it is possible not to have this very long title string 2000 times !?

share|improve this question
    
Are these links stored in a database? How are these links generated? –  mar10 Aug 25 '10 at 10:24
    
@mar10 these links are generated in Asp.Net by binding to a Datasource. So the number of rows depends of the number of rows my query returns. –  hoang Aug 25 '10 at 14:55
add comment

3 Answers

up vote 3 down vote accepted

One solution would be to use Javascript to add all the link titles once the page has loaded. This means the download for the user will be small - although depending on how long the string really is, it could eat up more memory. Another possible downside: search engines and some screenreaders may not see the link titles.

First add a class to your links. I kept it very short to keep the bloat as low as possible, but you may want a more descriptive class name.

<a id="1" class="t" href="goThere1">My 1st link</a>
[...]    
<a id="2000" class="t" href="goThere2000">My 2000th link</a>

Then add this Javascript (untested but should work):

<script type="text/javascript">
var titleString = "A very long string common to all my links that i would like to have only once in the file in order to gain weight."
var links = document.getElementsByClassName('t');
for ( var l in link )
{
    l.setAttribute('title', titleString);
}
</script>
share|improve this answer
    
Presuming you can't add the same Title to them all when you generate the page then I think this is the best solution. –  Toby Aug 25 '10 at 14:51
    
Good solution. If the OP only wants the titles for their mouse-over tooltip, it may be "lighter" to handle these with a floating div and mouseover/mouseout events rather than inserting the title tag on every row. –  JasonBirch Aug 25 '10 at 21:03
add comment

If your links are in a database then you have a couple of options, both of which are simple to do.

1) Update your database to include the title tag. If you don't already have a column for the title tag create one and then run this query: UPDATE tablename SET title = 'A very long string common to all my links that i would like to have only once in the file in order to gain weight';. Then whenever you dynamically generate the page(s) that use those links simply pull out and print the title with the rest of the link.

2) Since the text is the same for every link just hard code it right into your code. Here's a PHP example:

while ($row = mysql_fetch_assoc($result))
{

    echo '<a href="' . $row['url'] . '" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight">' . $row['anchor_text'] . '</a>';

}
share|improve this answer
    
Thank you John but maybe I wasn't clear : I'm not trying to gain weight in my source code, as I am obviously generating all those links. I'm trying to gain weight in the resulting page the browser will load. –  hoang Aug 25 '10 at 13:31
    
I don't know what you mean by "gaining weight". Care to explain? –  John Conde Aug 25 '10 at 13:50
    
I'm sure he means "lose weight", i.e. only have that string once in the output source, instead of 2000 times. –  DisgruntledGoat Aug 25 '10 at 14:06
    
Ok, gotcha. Thx. Your answer covers what I would have said then. –  John Conde Aug 25 '10 at 14:34
add comment

If you would like the title attribute to effective on each <A> element without putting the title against each link, then nest it in a DIV and assign the title attribute to the DIV.

For instance compare http://jsfiddle.net/tgew9/

<a id="1" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere1">My 1st link</a>
[...]    
<a id="2000" title="A very long string common to all my links that i would like to have only once in the file in order to gain weight" href="goThere2000">My 2000th link</a>?

with http://jsfiddle.net/tgew9/1/

<div title="A very long string common to all my links that i would like to have only once in the file in order to gain weight">
<a id="1" href="goThere1">My 1st link</a>
[...]    
<a id="2000" href="goThere2000">My 2000th link</a>
</div>
share|improve this answer
    
Solid idea, except that the links are all in separate table rows, not sure you can wrap a div around them. –  DisgruntledGoat Aug 25 '10 at 14:08
    
@DisgruntledGoat I just wrapped the whole table in a div and it works! –  hoang Aug 25 '10 at 15:05
1  
... Well in fact the tooltip appears wherever I point inside my table :/ –  hoang Aug 25 '10 at 15:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.