Just a small suggestion....
Maybe wrap the innerHTML with encodeURI(), or replace / escape quotes, otherwise if there is a quote in the HTML, it will break the title attribute.
Main Topics
Browse All TopicsI'm found a small JavaScript script that allows a user to create a HREF by selecting text string from a textarea and typing in the URL when prompted, but it does not add the title attribute to HREF.
Using PHP:
- It is possible to detect HERF's without a title?
- It is possible to add the title to the HREF?
If 1 & 2 above is yes, then is it possible to set the title of the HREF to the innerHTML of the HREF:
<a href="www.google.com" title="mywebsite name">my website name</a>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can certainly use the php string functions to search and modify the string with the URL.
For example, to find if a string "title=" exists in a longer string $url, you would use:
if (strpos($url, "title=") === false)
// note, be sure to use THREE ='s in the boolean comparision
// http://us2.php.net/manual/
To find the position of "title=" in a string $url, you would use:
$title_position = strpos($url, "title=");
// http://us2.php.net/manual/
To find the length of any string $url, you would use:
$string_length = strlen($url);
// http://us2.php.net/manual/
To pick out any piece of a string $url at a location you can identify, you would use substr( ):
$url_before_title = substr($url, strpos($url, "title=");
// http://us2.php.net/manual/
To insert "title=xxx" into a string, choose the place you want to insert it, then using strlen( ) and substr( ),
divide the string into $url1 and $url2 at the place you want to insert "title=xxx". Then concatenate the pieces using:
$new_url = $url1 . "title=xxx" . $url2;
These are the main php string functions that can help you check the URL and insert a title into it if there isn't one there already.
Check this code sample:
<html>
<head>
<title>Script Demo Gops</title>
<script type="text/javascript">
window.onload=function(){
var urls=document.links;
for(var i=0;i<urls.length;i++){
var titl=urls[i].getAttribute(
if(trim(titl).length==0) urls[i].title="Go to "+ urls[i].innerHTML;
}
}
function trim(str){
return str.replace(/(^\s+|\s+$)/g
}
</script>
</head>
<body>
<a href="#">Page1.com</a><br>
<a href="#">Page2.com</a><br>
<a href="#" title="Go to Page3">Page3.com</a><br>
<a href="#">Page4.com</a><br>
<a href="#">Page5.com</a><br>
<a href="#">Page6.com</a><br>
</body>
</html>
Still u can use my solution:
<html>
<head>
<title>Script Demo Gops</title>
<script type="text/javascript">
window.onload=function(){
var urls=document.links;
for(var i=0;i<urls.length;i++){
var titl=urls[i].getAttribute(
if(trim(titl).length==0) urls[i].title="Go to "+ urls[i].innerHTML;
}
}
function trim(str){
return str.replace(/(^\s+|\s+$)/g
}
</script>
</head>
<body>
<a href="#" title="">Page1.com</a><br>
<a href="#" title="">Page2.com</a><br>
<a href="#" title="Go to Page3">Page3.com</a><br>
<a href="#" title="">Page4.com</a><br>
<a href="#" title="">Page5.com</a><br>
<a href="#" title="">Page6.com</a><br>
</body>
</html>
gops,
Your solution does not add the title attribute to the end of the href tag.
I phyiscally want to add it. Take this example below:
<a href="http://www.domain.co
This is the url in my string before i insert it into the DB.
after i append the title attribute to the href tag, it should look like:
<a href="http://www.domain.co
Ellandrd
Business Accounts
Answer for Membership
by: RoonaanPosted on 2007-02-08 at 01:55:48ID: 18492418
Well probably you could just walk through to document.links collection and test/set titles:
e && document.links[i].innerHTM L) { L;
for(var i = 0; i < document.links.length; i++) {
if(!document.links[i].titl
document.links[i].title = "Visit " + document.links[i].innerHTM
}
}
-r-