Javascript Reference - JavaScript String link() Method








The link() method is used to display a string as a hyperlink by wrapping the string in the <a> tag.

Browser Support

link() Yes Yes Yes Yes Yes

Syntax

stringObject.link('url');

Parameter Values

Parameter Description
url Required. The URL to link to




Return Value

A string embedded in the <a> tag.

String with HTML tags

MethodOutput
anchor(name)<a name="name">string</a>
big()<big>string</big>
bold()<b>string</b
fixed()<tt>string</tt>
fontcolor(color)<font color="color">string</font>
fontsize(size)<font size="size">string
italics()<i>string</i>
link(url)<a href="url">string</a>
small()<small>string</small>
strike()<strike>string</strike>
sub()<sub>string</sub>
sup()<sup>string</sup>

The following code shows how to use the methods above.

<html>
<body>
    <script type="text/javascript">
      var myString = new String();
      myString = "java2s.com";
      document.writeln(myString.anchor("java2s.com"));
      document.writeln(myString.big());
      document.writeln(myString.bold());
      document.writeln(myString.fixed());
      document.writeln(myString.fontcolor(color));
      document.writeln(myString.fontsize(size));
      document.writeln(myString.italics());
      document.writeln(myString.link(url));
      document.writeln(myString.small());
      document.writeln(myString.strike());
      document.writeln(myString.sub());
      document.writeln(myString.sup());
<!--from w  w w . j av a2  s. c  o m-->
    </script>
</body>
</html>

Click to view the demo





Example


<!DOCTYPE html>
<html>
<body>
<script>
var txt = "tutorial!";
document.write("The original string: " + txt);
document.write("<p>Link: " + txt.link("http://www.java2s.com") + "</p>");
</script><!--from w  ww  .  jav  a2 s.  c o m-->
</body>
</html>

The code above is rendered as follows: