ekyo-g
Last Updated: October 12, 2018
·
1.867K
· idered

[JS] Not well known link() method

Probably every jQuery developer know how to create link with it:

$('<a href="http://google.com"/>').text('Google');

or:

$('<a href="http://google.com">Google</a>');

But there's also a little know link() method:

'Google'.link('http://google.com');

4 Responses
Add your response

3418

Interesting find! Is there a spec for the function somewhere? I wonder what's the browser support...

over 1 year ago ·
4214

Wow!

W3Schools says it's supported in "All Major Browsers". A quick test in Firefox, IE7, and Chrome shows it works in all of those.

I never expected a method like that on the String object. It's very Pythonic...

over 1 year ago ·
4215

I think the jQuery reference to create links here is fairly irrelevant; jQuery is a popular library, while the actual subject of the post is about a (non-standard) feature.

There's a bunch more HTML-creation string prototype functions in the String prototype, which you can find on this MDN page: String.prototype

over 1 year ago ·
4237

There are many other HTML wrapper methods in JS besides just link() too.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String#HTML_wrapper_methods

over 1 year ago ·