Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format/Safe string for "title" attribute in anchor

I have a function that builds an anchor tag. The function recieves the URL, Title as parameters. The problem is that sometime the text includes quotation marks and this results in a anchor tag generated with syntax errors.

Which is the best way to solve this problems? Is there any function that parses the text into a safe string, in this case, for the title attribute.

Otherwise I can check the string and strip all quotation marks, but I would like know if there is a better way to do this, e.g there might be some other characters that can crash my function as well.

like image 865
tif Avatar asked Apr 18 '11 14:04

tif


People also ask

How do you style title attributes?

You can't style an actual title attribute How the text in the title attribute is displayed is defined by the browser and varies from browser to browser. It's not possible for a webpage to apply any style to the tooltip that the browser displays based on the title attribute.

Can we add title to anchor tag?

The objective of this technique is to demonstrate how to use a title attribute on an anchor element to provide additional text describing a link. The title attribute is used to provide additional information to help clarify or further describe the purpose of a link.

What sort of information is appropriate for use with the title attribute?

Appropriate information to include in a link title can be: name of the site the link will lead to (if different from the current site) name of the subsite the link will lead to (if staying within the current site but moving to a different part of the site)

How do you style a tooltip title?

The tooltip is automatically generated by web browsers, it actually can not be removed or changed. To change the tooltip style, we need to add a tooltip text to a different attribute, for example data-title , then use CSS code to create and customise the style. In WordPress, you can add the CSS code to your theme CSS.


1 Answers

Actually you want to use HttpUtility.HtmlAttributeEncode to encode your title attribute. The other encoders will do more work (and have different uses) whereas this one only escapes ", &, and < to generate a valid text for an attribute.

Example: This is a <"test"> & something else. becomes This is a &lt;&quot;Test&quot;> &amp; something else.

like image 111
Joshua Avatar answered Sep 21 '22 02:09

Joshua