Links in JS

Discussion in 'JavaScript' started by uca, Nov 21, 2008.

  1. #1
    What's (or what are the options for) the exact syntax of a javascript link?

    Thanks!

    :)
     
    uca, Nov 21, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are no links in javascript. You can open a new window, change the current location or reload it, you can create an html element which is a link and lots of other stuff, but there is no 'javascript link'. I imagine the thing you are asking about is - how to create <a> link with javascript. Here's the simplest way to do that:
    
    //create the <a> tag 
    var myLink = document.createElement('a');
    a.href = 'http://mysite.com/';
    a.innerHTML = 'link text';
    
    //and append it to the page body
    document.body.appendChild(myLink);
    
    Code (markup):
     
    xlcho, Nov 25, 2008 IP