Sign in to follow this  

[web] Adding Event Handler AJAX

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been messing with AJAX recently and am currently trying to add an event handler in the pageload script function. It works fine in IE, but nothing I do seems to work in Firefox. I have tried: $addHandler($get("btn1"), 'click', testJava); document.getElementById('btn1').attachEvent('click', testJava); document.getElementById('btn1').addEventListener('click', testJava, false); document.getElementById('btn1').onclick = testJava; Like I said, all of these, as far as I remember, worked fine in IE, but none worked in Firefox. The event handler is being added within the pageLoad() function.
<script type="text/javascript">
        function pageLoad()
        {
            var e1 = document.getElementById('btn1');   
            e1.addEventListener('onclick', testJava, false);     
        }
        
        
        function testJava()
        {
            var u = $get('user name').value;
            alert('lalala');
        }
</script>

Am I missing something? Does Firefox require a call to the pageLoad function or something to that nature?

Share this post


Link to post
Share on other sites
I've always used the fourth version you listed. I see you have this in a "pageLoad" function, how are you ensuring this function is executed?

Also, watch your terminology. You have no "AJAX" here, only javascript and DOM.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
I've always used the fourth version you listed. I see you have this in a "pageLoad" function, how are you ensuring this function is executed?

Also, watch your terminology. You have no "AJAX" here, only javascript and DOM.


True, it isn't exactly an AJAX issue.

I placed an alert within the pageLoad to ensure the function is executed, it pops up in both IE and Firefox.

Share this post


Link to post
Share on other sites
So, problem solved, I guess.

I took out this line:

var u = document.getElementById("user").value;

It didn't like having value on there. When using value, it works fine in IE, it is able to get the data that I want, but with Firefox, it seems to 'crash out' of the function as soon as I hit that line. Putting an alert before the value variable definition causes an alert as desired, but it does not go any further.

What should I be using in place of value?

I am trying to access the text of a textbox.

<input type="text" name="user" />

Share this post


Link to post
Share on other sites
Your pageLoad function won't call itself ("pageLoad" is not a convention like for example the main-function in C). You'll have to ensure that it's called...for example like this:


window.onload = pageLoad;


Another thing: the DOM event model doesn't use the "on"-prefix for events, so

e1.addEventListener('onclick', testJava, false);


should be

e1.addEventListener('click', testJava, false);


AND addEventListener is currently not supported in IE. IE uses attachEvent while addEventListener is the DOM way of doing it that is supported across (most) browsers. The onclick-syntax is also supported everywhere but it's non-standard.

These browser inconsistencies is why you should use a framework. It doesn't have to be GWT though, because that will abstact all the javascript away from you and...javascript is actually a pretty cool language, once you understand its inner workings.

Also, check out Quirksmode for tons of information regarding browser quirks. The website also has a page explaining a cross-browser way of using event registration (see here).

EDIT: In order for getElementById to work, your element needs to have an ID. Using getElementById with NAME is non-standard and only works in IE. So you need to change

<input type="text" name="user" />


to

<input type="text" id="user" />



EDIT: pageLoad seems to be a convention in ASP, so you might not need the window.onload after all.

Share this post


Link to post
Share on other sites
Thanks a lot, Harry. I overlooked the use of 'name' in that declaration.

I used quirkmode a lot when trying to figure these situations out, it's a good source.

Share this post


Link to post
Share on other sites

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this