Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

How do I create a link using javascript

By: a guest on Mar 22nd, 2012  |  syntax: None  |  size: 1.76 KB  |  views: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <html>
  2. <head></head>
  3. <body>
  4. <script type="text/javascript">
  5.  
  6. var a = document.createElement('a');
  7. a.title = "my title text";
  8. a.innerHTML = a.title;
  9. a.href = "http://example.com";
  10. document.body.appendChild(a);
  11.  
  12. </script>
  13. </body>
  14. </html>
  15.        
  16. var link = "http://google.com";
  17. var element = document.createElement("a");
  18. element.setAttribute("href", link);
  19. element.innerHTML = "your text";
  20.  
  21. // and append it to where you'd like it to go:
  22. document.body.appendChild(element);
  23.        
  24. document.write("<a href='" + link + "'>" + text + "</a>");
  25.        
  26. document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';
  27.        
  28. <script type="text/javascript">
  29. //note that this case can be used only inside the "body" element
  30. document.write('<a href="'+desiredLink+'">'+desiredText+'</a>');
  31. </script>
  32.        
  33. $('<a href="'+desiredLink+'">'+desiredText+'</a>').appendTo($('body'));
  34.        
  35. $('body').append($('<a href="'+desiredLink+'">'+desiredText+'</a>'));
  36.        
  37. var a = $('<a />');
  38. a.attr('href',desiredLink);
  39. a.text(desiredText);
  40. $('body').append(a);
  41.        
  42. desiredLink
  43.        
  44. var anchorElem = document.createElement('a');
  45.    anchorElem.setAttribute("href", yourLink);
  46.    anchorElem.innerHTML = yourLinkText;
  47.  
  48.    document.body.appendChild(anchorElem); // append your new link to the body
  49.        
  50. <script language="javascript">
  51. <!--
  52. document.write("<a href="www.example.com">");
  53. document.write("Your Title");
  54. document.write("</a>");
  55. //-->
  56. </script>
  57.        
  58. <script type="text/javascript">
  59. document.write('Your Title'.link('http://www.example.com'));
  60. </script>
  61.        
  62. <script type="text/javascript">
  63. newlink = document.createElement('a');
  64. newlink.innerHTML = 'Google';
  65. newlink.setAttribute('title', 'Google');
  66. newlink.setAttribute('href', 'http://google.com');
  67. document.body.appendChild(newlink);
  68. </script>