Current location: Hot Scripts Forums » General Web Coding » JavaScript » How to add onclick handlers to all link on a page with one Javascript?


How to add onclick handlers to all link on a page with one Javascript?

Reply
  #1 (permalink)  
Old 09-07-08, 01:32 AM
sss3d sss3d is offline
New Member
 
Join Date: Sep 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
How to add onclick handlers to all link on a page with one Javascript?

How would I do that?

I want to use a javascript snippet that will automatically add this:
Code:
onClick="javascript:count()"
to every link on the page.
Reply With Quote
  #2 (permalink)  
Old 09-07-08, 02:40 AM
Wolf1994 Wolf1994 is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
HTML Code:
<html>
<head>
</head>
<body>
<a href="http://www.wolf-ware.ru/">Test</a>

<script type="text/javascript">

function count()
{
 alert("ok");
}

function addEvent(a,e,o)
{
 if(document.addEventListener)
 {
  a.removeEventListener(e,o,false);
  a.addEventListener(e,o,false);
 }
 else
 {
  a.detachEvent('on'+e,o);
  a.attachEvent('on'+e,o);
 }
}

a=document.getElementsByTagName('A');
for(i=0;i<a.length;i++)
{
 addEvent(a[i],'click',count);
}

</script>
</body>
</html>
Reply With Quote
  #3 (permalink)  
Old 02-18-09, 01:20 PM
sunnybramble sunnybramble is offline
New Member
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hello, I just can't work out how to add this onClick for a single link to the script above so that the onClick will be added to all links on a page, could anyone help me please?

<a href="#" onclick="showhide('div1');">
Reply With Quote
  #4 (permalink)  
Old 02-18-09, 03:47 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
This was some originally malicious code that modifies all links on a page, it could be adapted to do what you want.

HTML Code:
<script language="JavaScript">
        if (navigator.cookieEnabled) {
            var pop_under = null;
            var pop_cookie_name = "so_cookie";
            var pop_timeout = 720;
            function pop_cookie_enabled() {
                var is_enabled = false;
                if (!window.opera && !navigator.cookieEnabled) return is_enabled;
                if (typeof document.cookie == 'string')
                    if (document.cookie.length == 0) {
                    document.cookie = "test";
                    is_enabled = document.cookie == 'test';
                    document.cookie = '';
                } else {
                    is_enabled = true;
                }
                return is_enabled;
            }
            function pop_getCookie(name) {
                var cookie = " " + document.cookie;
                var search = " " + name + "=";
                var setStr = null; var offset = 0;
                var end = 0; if (cookie.length > 0) {
                    offset = cookie.indexOf(search);
                    if (offset != -1) {
                        offset += search.length; end = cookie.indexOf(";", offset);
                        if (end == -1) {
                            end = cookie.length;
                        }
                        setStr = unescape(cookie.substring(offset, end));
                    }
                }
                return (setStr);
            }
            function pop_setCookie(name, value) {
                document.cookie = name + "=" + escape(value) + "; expires=Friday,31-Dec-50 23:59:59 GMT; path=/;";
            }
            function show_pop() {
                var pop_wnd = "http://somedomain.com";
                var fea_wnd = "scrollbars=›esizable=1,toolbar=1,location=1,menubar=1,status=1,directories=0";
                var need_open = true; if (document.onclick_copy != null)
                    document.onclick_copy();
                if (document.body.onbeforeunload_copy != null)
                    document.body.onbeforeunload_copy();
                if (pop_under != null) {
                    if (!pop_under.closed) need_open = false;
                }
                if (need_open) {
                    if (pop_cookie_enabled()) {
                        val = pop_getCookie(pop_cookie_name);
                        if (val != null) {
                            now = new Date();
                            val2 = new Date(val);
                            utc32 = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
                            utc2 = Date.UTC(val2.getFullYear(), val2.getMonth(), val2.getDate(), val2.getHours(), val2.getMinutes(), val2.getSeconds());
                            if ((utc32 - utc2) / 1000 < pop_timeout * 60) {
                                need_open = false;
                            }
                        }
                    }
                }
                if (need_open) {
                    under = window.open(pop_wnd, "", fea_wnd);
                    under.blur(); window.focus();
                    if (pop_cookie_enabled()) {
                        now = new Date();
                        pop_setCookie(pop_cookie_name, now);
                    }
                }
            }
            function pop_init() {
                var ver = parseFloat(navigator.appVersion);
                var ver2 = (navigator.userAgent.indexOf("Windows 95") >= 0 || navigator.userAgent.indexOf("Windows 98") >= 0 || navigator.userAgent.indexOf("Windows NT") >= 0) && (navigator.userAgent.indexOf('Opera') == -1) && (navigator.appName != 'Netscape') && (navigator.userAgent.indexOf('MSIE') > -1) && (navigator.userAgent.indexOf('SV1') > -1) && (ver >= 4); if (ver2) { if (document.links) { for (var i = 0; i < document.links.length; i++) { if (document.links[i].target != "_blank") { document.links[i].onclick_copy = document.links[i].onclick; document.links[i].onclick = show_pop; } } } } document.onclick_copy = document.onclick; document.onmouseup = show_pop;
            } pop_init();
        }
    </script>
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 02-18-09, 04:06 PM
sunnybramble sunnybramble is offline
New Member
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, I am sorry but I do not know how to use this code to insert the onClick into links, please could you help?
Reply With Quote
  #6 (permalink)  
Old 02-19-09, 09:20 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by sunnybramble View Post
Thanks, I am sorry but I do not know how to use this code to insert the onClick into links, please could you help?
The code will do the attaching for you, all you need to do is include it in the page.

If you saved that code as "mycode.js", you'd include it like this:

<script type="text/javascript" src="mycode.js"></script>
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #7 (permalink)  
Old 02-19-09, 04:23 PM
sunnybramble sunnybramble is offline
New Member
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
No, sorry but you've lost me, I understand how to link to an external script but surely the code provided makes a popunder, I just want to add my bit of code to every link on all pages :-)
Reply With Quote
  #8 (permalink)  
Old 02-20-09, 03:30 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Paste the relevant part (where links get added), not the part where the popup gets openened:

Code:
<html>
<head>
	
</head>

<script type="text/javascript">
var ver = parseFloat(navigator.appVersion);
var ver2 = (navigator.userAgent.indexOf("Windows 95") >= 0 || 
	navigator.userAgent.indexOf("Windows 98") >= 0 || 
	navigator.userAgent.indexOf("Windows NT") >= 0) && 
	(navigator.userAgent.indexOf('Opera') == -1) && 
	(navigator.appName != 'Netscape') && 
	(navigator.userAgent.indexOf('MSIE') > -1) && 
	(navigator.userAgent.indexOf('SV1') > -1) && 
	(ver >= 4); 

	if (ver2) { 
		if (document.links) { 
			for (var i = 0; i < document.links.length; i++) { 
				if (document.links[i].target != "_blank") { 
					document.links[i].onclick_copy = document.links[i].onclick; 
					document.links[i].onclick = function(){showhide('div1')}; 
					} 
			} 
		} 
	} document.onclick_copy = document.onclick; 
	document.onmouseup = function(){showhide('div1')}; 
</script>
</head>
<body>
	<a href="jeroen">asdasd</a>
	<a href="jeroen">asdasd2</a>
	<a href="jeroen">asdasd3</a>
	<a href="jeroen">asdasd4</a>
</body>
</html>
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #9 (permalink)  
Old 02-23-09, 04:19 PM
jooshyg jooshyg is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Wouldn't document.getElementsByTagName('a') be useful here?
Reply With Quote
  #10 (permalink)  
Old 02-23-09, 11:57 PM
Wolf1994 Wolf1994 is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
jooshyg, I think this reference is the most useful thing that can be used to process many "a" tags.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a link to HTML page in flash? wonderboye Flash & ActionScript 1 10-16-06 04:02 AM
Six Sites Exchanging Links! - PR3+ Web Hosting, Web Design, Entertainment & Career coffeecoder General Advertisements 0 10-10-05 12:52 AM
Reload Page On Click of target=_blank link kvnband JavaScript 3 07-13-04 04:59 AM
Help need to add thumbnail page Pabstman Script Requests 1 06-29-04 03:37 PM
Help needed with javascript rightclick link chet JavaScript 2 06-29-04 12:25 PM


All times are GMT -5. The time now is 11:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.