Using setAttribute('href') to set link for <a> - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

Using setAttribute('href') to set link for <a>

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  ww w  .  j a v a 2 s.  c om*/
var o = document.getElementById("LinkFBShareLink");
var link = "foo";
var title = "bar";
var fbURL = 'https://www.facebook.com/sharer/sharer.php?u=' + link + '&title=' + title;
o.setAttribute('href', fbURL);
    }

      </script> 
   </head> 
   <body> 
      <a href="#" id="LinkFBShareLink">testing</a>  
   </body>
</html>

Related Tutorials