JavaScript link method primer example (add hyperlinks to strings)

  • 2020-03-30 04:08:56
  • OfStack

The link method returns the (italic) string defined using the HTML a tag attribute. The syntax is as follows:


str_object.link( url )

Parameter description:

parameter instructions
str_object String (object) to manipulate
url A necessity. String to link URL Address, full format.

Note: this method does not conform to ECMA standards and is not recommended.

Link method example


<script language="JavaScript">
var str = "www.jb51.net";
document.write( str.link( "//www.jb51.net" ) );
</script>

Run the example and output:


www.jb51.net

Tip: this method returns a hyperlink string defined using an HTML a tag, meaning that you cannot directly change a string to a hyperlink string using this method. If you want to dynamically change the element font to a hyperlink string, you can refer to the following example to extend the link method:

Change page element font to hyperlink


<html> <script language="JavaScript"> function addUrl( obj ){
    obj.innerHTML = obj.innerHTML.link( "//www.jb51.net" );
} </script> <body>
<p ondblclick="addUrl(this);">
www.jb51.net
</p>
</body>
</html>

In this example, you can add a hyperlink to the string www.jb51.net by double-clicking.


Related articles: