Mobile app version of vmapp.org
Login or Join
Phylliss660

: Is it possible to make all html links to share the same title? 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"

@Phylliss660

Posted in: #Css #Html

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

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Phylliss660

3 Comments

Sorted by latest first Latest Oldest Best

 

@Yeniel560

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>

10% popularity Vote Up Vote Down


 

@Sarah324

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>';

}

10% popularity Vote Up Vote Down


 

@Nimeshi995

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme