473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

addEventListener Problem

Using Firefox, I am trying to access a link within my HTML document, then use addEventListener() to add a click event to that link. I am using two external JS files.

ajaxModal.js has the listener function which will be called when the link is clicked.

events.js will have all the addEventListener() functions.

ajaxModal.js contents:

Expand|Select|Wrap|Line Numbers
  1. function showAjaxModal()
  2. {
  3. xmlHttp = GetXmlHttpObject();
  4. if(xmlHttp==null)
  5.   {
  6.   alert ("Browser does not support HTTP Request");
  7.   return;
  8.   }
  9. alert('Accessed showAjaxModal!');
  10. }
  11. // Detect XMLHTTP
  12. function GetXmlHttpObject()
  13. {
  14. var xmlHttp=null;
  15. try
  16.  {
  17.  // Firefox, Opera 8.0+, Safari
  18.  xmlHttp=new XMLHttpRequest();
  19.  }
  20. catch (e)
  21.  {
  22.  // Internet Explorer
  23.  try
  24.   {
  25.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  26.   }
  27.  catch (e)
  28.   {
  29.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  30.   }
  31.  }
  32. return xmlHttp;
  33. }

events.js contents:


Expand|Select|Wrap|Line Numbers
  1. var triggerLink = document.getElementById('page1');
  2. triggerLink.addEventListener('click',showAjaxModal,false);
HTML:

[HTML]
<html>
<head>
...
<script type="text/javascript" src="scripts/events.js"></script>
<script type="text/javascript" src="scripts/ajaxModal.js"></script>
...
</head>
<body>
...
<div id="nav">
<ul>
<li><a href="index.html" onclick="showAjaxModal(); return false">Home</a></li>
<li><a id="page1" href="page1.html">Page 1</a></li>
<li><a href="page2.html">Page 2</a></li>
<li><a href="page3.html">Page 3</a></li>
</ul>
</div>
...
</body>
</html>
[/HTML]

The inline event handler works, and runs showAjaxModal()....

the second link with ID 'page1' does not run showAjaxModal(), spite the fact that I added a click event to it with addEventListener in events.js.

I am almost sure this is a beginner mistake. How can I get the addEventListener() to work on this page?

Thanks,

Jeremy
Apr 30 '07 #1
6 3684
iam_clint
1,208 Expert 1GB
why not

triggerLink.addEventListener('click',showAjaxModal ,false);

to
triggerLink.onClick = function() { showAjaxModal(); }
Apr 30 '07 #2
why not

triggerLink.addEventListener('click',showAjaxModal ,false);

to
triggerLink.onClick = function() { showAjaxModal(); }
I am getting the same result with your suggested method. I have tried both. Both methods for registering the event do not work. I click the link, and it just loads the href as normal, and ignores any click events ... I am trying to find a typo in my scripts, but everything looks legit to my knowledge. I am hoping someone can find my error, because I cannot see it.

I am choosing to learn addEventListener() because I want to get a jump start on the addEventListener() method. Using the 'traditional' method of registering event handlers doesn't allow to add more than one event handler to the same event on the same element (not that I would need to in this case) which is a drawback (see ppk-Quirksmode http://www.quirksmode.org/js/events_tradmod.html). I have decided that event registration with addEventListener() is supported enough in modern browsers for my purposes (with the condition for using IE attachEvent() of course).

However, I would also like to get it working using your suggested method, because it is not working that way either.

I tried,

Expand|Select|Wrap|Line Numbers
  1. var triggerLink = document.getElementById('page1');
  2. triggerLink.onClick = function() { showAjaxModal(); }
with no success.

Thanks for your suggestion.
Apr 30 '07 #3
iam_clint
1,208 Expert 1GB
you need it to drop the href and make your javascript goto the link after it pulls the javascript function.


window.location =

<a href="#">
Apr 30 '07 #4
you need it to drop the href and make your javascript goto the link after it pulls the javascript function.


window.location =

<a href="#">


I could be way off base with this, but I am very confused. I want the href to be followed if JS is off, but if JS is on, I want the showAjaxModal to execute. When using your suggestion of <a href="#">, this link becomes broken to users who do not have javascript turned on. Is there a way to get around that, other than using an inline event handler with return false (ie. <a href="page1.html" onclick="func(); return false">Page 1</a>) ?

This inline method is working, but I prefer to create a link that does not use inline event handling, because ideally I want to maintain a pure separation of xhtml content and javascript behavior. Quirksmode says, "Although the inline event registration model is ancient and reliable, it has one serious drawback. It requires you to write JavaScript behavior code in your XHTML structure layer, where it doesn't belong." [1 ]

Is there a way to make this link accessible to folks with JS turned off, without using inline events?

Thanks again,
Jeremy
Apr 30 '07 #5
Seems like I may be confused. I'll do a little reading. It's time to get our JS reference books for a while. Let me know if you have any advice.

Thanks!

Jeremy
May 1 '07 #6
iam_clint
1,208 Expert 1GB
yes

have the javascript write all of your links to # then if javascript it is turned off you don't have to worry about it.
May 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: PJ | last post by:
Is it possible to extend the Node object so that the DOM function addEventListener can be used w/ IE? Does anyone have an example of this? Thanks, Paul
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
2
by: philjhanna | last post by:
Hi, Does anyone know why I can't add return false with addEventListener in firefox (1.0.6). This demonstrates the problem If return false is added to onmousedown then you can drag over the...
4
by: Liming | last post by:
Hello all, I have a custom class object with some prototype methods like setKeyDownEvent, etc. The problem is on my webpage, after I instantiate the class, I try to do .addEventLister() to a...
5
by: Bert | last post by:
Hello, I'm having some problems with creating a script that works on both Mozilla browsers as IE. I want to change the background color of textareas and input text fields as soon as somebody...
2
by: Terry | last post by:
Hi all, I have been googling this problem for many hours... I have the following greasemonkey script; (function() { if (true) { // Change these to match your setup var sabcomputer =...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.