code_junkie

What's the best way to replace links with JS functions?

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. var link = document.getElementById('confirmToFollow');
  2.  
  3. link.onclick = function () {
  4. return confirm("Are you sure?");
  5. };
  6.  
  7. <a href="#" id="confirmToFollow"...
  8.  
  9. var link = document.getElementById('confirmToFollow');
  10.  
  11. link.onclick = function () {
  12. if( confirm("Are you sure?") ) {
  13. window.location = "http://www.stackoverflow.com/";
  14. }
  15. return false;
  16. };
  17.  
  18. var allLinks = document.getElementsByTagName('a');
  19. for (var i=0; i < allLinks.length; i++) {
  20. allLinks[i].onclick = function () {
  21. return confirm("Are you sure?");
  22. };
  23. }
  24.  
  25. <a href="mypage.html" onclick="...
  26.  
  27. <a href="javascript: confirmLink() ...
  28.  
  29. <a href="#" onClick="goThere(); return false;">Go to new page</a>`
  30.  
  31. function goThere()
  32. {
  33. if( confirm("Are you sure?") )
  34. {
  35. window.location.href="newPage.aspx";
  36. }
  37. }
  38.  
  39. <a href="http://..." onclick="return confirm('are you sure?')">text</a>
  40.  
  41. jQuery(document).ready(
  42. jQuery("a").click(){
  43. //you'd place here your extra logic
  44. document.location.href = this.href;
  45. }
  46. );
  47.  
  48. $("a").click(function() {if(confirm('yadda yadda')) event.stopPropagation();});
  49.  
  50. $(".conf").click(function() {if(confirm('yadda yadda')) event.stopPropagation();});
  51.  
  52. // dangerous - warnings on potentially harmful actions
  53. // usage: add class="dangerous" to <a>, <input> or <button>
  54. // Optionally add custom confirmation message to title attribute
  55.  
  56. function dangerous_bind() {
  57. var lists= [
  58. document.getElementsByTagName('input'),
  59. document.getElementsByTagName('button'),
  60. document.getElementsByTagName('a')
  61. ];
  62. for (var listi= lists.length; listi-->0;) { var els= lists[listi];
  63. for (var eli= els.length; eli-->0;) { var el= els[eli];
  64. if (array_contains(el.className.split(' '), 'dangerous'))
  65. el.onclick= dangerous_click;
  66. }
  67. }
  68. }
  69.  
  70. function array_contains(a, x) {
  71. for (var i= a.length; i-->0;)
  72. if (a[i]==x) return true;
  73. return false;
  74. }
  75.  
  76. function dangerous_click() {
  77. return confirm(this.title || 'Are you sure?');
  78. }
  79.  
  80. dangerous_bind();
  81.  
  82. var links = document.getElementsByTagName('A');
  83.  
  84. for (var i = 0; i < links.length; i++)
  85. {
  86. links[i].onclick = function ()
  87. {
  88. return Confirm("Are you sure?");
  89. }
  90. }
Add Comment
Please, Sign In to add comment