Search code examples
javascripthyperlinktarget

passing a target to the js link() method


How do I pass a target, _blank, to the js link method?

x = "my link to google"
x.link("www.google.com")

<a href="www.google.com">my link to google</a>

if its not possible is there an alternative method I could use?


Solution

  • You can't; the String.link method is ancient and mostly deprecated. Construct the link using DOM methods instead:

    var link = document.createElement("a");
    link.setAttribute("href", "http://www.google.com/");
    link.setAttribute("target", "_blank");
    link.appendChild(document.createTextNode("my link to google"));
    ...
    // this, or whatever else you want to do to add it to the document:
    document.getElementById("something").appendChild(link);
    

    Either that, or just build up the string yourself. String.link isn't doing much anyway.