Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onclick event problem

Status
Not open for further replies.

rsacchi

Programmer
Oct 15, 2006
4
IT
The code below (userJS code) simply create a link, and If click on that link, it pops up an alert.
It seems to work, but have a problem:
the alert associated with onclick event pops automatically when the page starts... I really don't know how it may happen...
I'm using FF, maybe it's a setting browser problem...

Here's the userJS code:

Code:
// ==UserScript==
// @name           try
// @namespace      [URL unfurl="true"]http://nosite.com[/URL]
// @description    no description
// @include        [URL unfurl="true"]http://www.indexmundi.com/*[/URL]
// ==/UserScript==
	
	var temp = document.getElementsByTagName('BODY');
	var linkz = document.createElement('A');	
      linkz.setAttribute('href', '[URL unfurl="true"]http://link.com');[/URL]
      linkz.setAttribute('name', 'clickable link');
      linkz.appendChild(document.createTextNode('This is the link'));
		linkz.onclick = new Function(Do());
		temp[0].appendChild(document.createElement('BR'));
		temp[0].appendChild(document.createElement('BR'));
		temp[0].appendChild(linkz);


function Do() {
	alert('Hello!');
}

Thanks for help

P.s: As you can notice, I'm not referring to onClick javascript function,but to "onclick" event of DOM element.

Sacchi
 
>linkz.onclick = new Function(Do());
[tt]linkz.onclick = Do;[/tt]
 
It doesn't work in my browser.
The javascript console write me:
Component not available (at line 13)

I get the same error if I write:

linkz.onclick = function() { Do(); };
linkz.onclick = function() { Do; };
OR
linkz.onclick = function() { alert("Hello"); };

but if I write:
linkEsper.onclick = alert('Hello');

alert pops only when page starts.
 
Because you have no idea how important is the placement of the fragment you show the forum. It must be after the body tag, or you have to wrap the whole thing within the onload event.
 
I used BODY tag just for an example.
Also if I use:
Code:
var temp = document.getElementsByTagName('DIV');
still doesn't work :(

If I put all the code into a function, and call her by
window.onload (or body[0].onload) I've the same result: it pops at the start.

Could you try my code just to see if it works for you?

Thanks
 
A very unorthodox practice.
[tt]
<body></body>
<script language="javascript">
// ==UserScript==
// @name try
// @namespace // @description no description
// @include // ==/UserScript==

var temp = document.getElementsByTagName('BODY');
var linkz = document.createElement('A');
linkz.setAttribute('href', ' linkz.setAttribute('name', 'clickable link');
linkz.appendChild(document.createTextNode('This is the link'));
linkz.onclick = Do;
temp[0].appendChild(document.createElement('BR'));
temp[0].appendChild(document.createElement('BR'));
temp[0].appendChild(linkz);


function Do() {
alert('Hello!');
}
</script>
[/tt]
 
Resolved!

I'm not in a HTML, but in a javascript page.
Hawever thank for your help.

The working line was:
linkz.addEventListener('clic',Do,false);


 
>I'm not in a HTML, but in a javascript page.
?who else isn't? but what is a javascript page?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top