简体   繁体   中英

a href onclick event is not working if there a rel attribute defined on it

I have a onclick event for a href , but if a define a rel the function is not getting called

<a href="http://somewebsite" 
   onclick="javascript:someFunction('somevalue');" 
   name="top" 
   rel="somevalue">testing </a>

If I remove the rel property the onclick is working just fine.

What's up with that?

I'm not sure this is really an answer, but really more of a debugging diagnostic. Plus, it was getting a bit long for use in the comments section.

  1. There are no JS errors for me in either Chrome 7 or FF 3.6.8

  2. There are no apparent unclosed quotes in the anchor tag

  3. While the rel tag is not directly supported by any modern browser (but rather used by search spiders), the consensus is that it shouldn't affect events. In fact, per some user comments, such @James Kovaks, it works fine in his tests .

  4. From the Chrome JS console, the function trackForGA is visible, and executable without error.

  5. Attaching an event with jQuery, and then clicking, fires the event as expected. This tells us the click event is in fact being fired by the browser.

code:

  $('#countrytabs li:first').click(trackForGA); // click overview tab 

Suggested next steps:

  • Try other event types, to see if they are affected as well - such as onmouseover.
  • Begin removing JavaScript that is not related to this particular event. Specifically, the only JS you want to remain is the trackForGa function and the onclick JS. Even remove externally loaded JS, such as jQuery (you can presumably do this on a local test/dev box)
  • If the problem persists, remove trackForGa and instead just do an alert() from the onclick event.
  • If the problem persists, run your site through an HTML validator ( http://validator.w3.org/ )
  • If the problem persists after fixing validation problems, begin removing unrelated HTML tags and content. Systematically remove items until the click is firing as expected to identify a potential culprit.

Also, you never need to specify " javascript: " in an onclick. The only thing that can follow is JavaScript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM