Jump to content

onclick doesn't work


nasi

Recommended Posts

HiI have a web page with some links (there is no id assigned to these links). I want to add onclick feature to some of these links. by looking at the source code of that webpage I realized that I can add onclick to the links using the following code:

doc = doc.replace(/&sa=N\"/g, "&sa=N\" onClick=\"alertsomething();return false;\"");function alertsomething(){alert('hello');}

but when the webpage is loaded and I click on these links the "alertsomthing" function doesn't work. it generates the following page:

Object not found!The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.If you think this is a server error, please contact the webmaster.Error 404 localhost 12/05/2011 10:00:29 AM Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

I checked the following code and it works properly

doc = doc.replace(/&sa=N\"/g, "&sa=N\" onClick=\"alert("hello");return false;\"");

I am using IE7.

Link to comment
Share on other sites

It looks like you want to get into some serious stuff here, so let's get serious. Adding quoted onclick handlers to links is NOT serious. DOM techniques are serious.The following is probably not what you want, because it targets ALL links, but it's a foundation:

function alertsomething () {   alert("Hello");   return false; // important}var links = document.getElementsByTagName('a');var i, len = links.length;for (var i = 0; i < len; ++i) {   links[i].onclick = alertsomething; // note the absence of parens () -- this is correct}

It seems that you are adding a custom sa attribute to your links to mark which ones get this treatment. That should work, though it will cause validation problems. Still, let's work with what you seem to have. I'm just adding a hook to the for-loop:

var links = document.getElementsByTagName('a');var i, sa, len = links.length;for (var i = 0; i < len; ++i) {   sa = links[i].getAttribute('sa');   if (sa == "N") {	  links[i].onclick = alertsomething;   }}

We have to use the getAttribute method because that is the only reliable way to get the value of custom attribute, such as your sa attribute. If sa were a standard attribute, we could have used links.sa -- but it's not, so we can't.OTOH, if your click handler ALWAYS keeps the kink from functioning as a link, then I presume it has no href attribute or that it has no value. To validate, a link element must have an href attribute, so lets assume those links look like this (I'm eliminating the sa attribute):

<a href="">Click me</a>

Now we can tweak our function a little:

var links = document.getElementsByTagName('a');var i, sa, len = links.length;for (var i = 0; i < len; ++i) {   sa = links[i].href;   if (sa == "") {	  links[i].onclick = alertsomething;   }}

There might be other things to do depending on your circumstances, but this should get your started.

Link to comment
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...