www.webdeveloper.com
Results 1 to 9 of 9

Thread: Help, ajax object for ie doesn't seem to support attachEvent method

  1. #1
    Join Date
    Oct 2008
    Posts
    5

    Help, ajax object for ie doesn't seem to support attachEvent method

    help, i tried adding two event listeners to an ajax object in ie (specifically ie6) but it doesn't seem to support the attachEvent method.

    no problem with firefox. addEventListener works. but how about ie?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,666
    In IE6, the standard XMLHttpRequest object was not available. You had to use the ActiveX object, instead. This ActiveX object would be outside of the browser DOM and, therefore, I would not expect to be able to attach events to it as you would regular DOM objects. What events are you trying to listen for?

  3. #3
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    What are you trying to Listen for?

    Eric

  4. #4
    Join Date
    Feb 2008
    Posts
    1,666
    If the only event you're interested in is the onreadystatechange event, then assign it as follows (works in all browsers):
    Code:
    ajaxObj.onreadystatechange = listenerFunction;
    If you want to remove the listener, you can do this (works in all browsers):
    Code:
    ajaxObj.onreadystatechange = function() { return true };

  5. #5
    Join Date
    Oct 2008
    Posts
    5
    hi, thanks for your reply.

    i know it would be easier if i would just attach one listener function to an event and that particular function would do all that i need to do. but i have a better reason why i opted not to.

    i was trying to create a library file that would do some common functionality and i would want to return the ajax object cause the caller might want to do something else with it. to do this they would add another event listener to it.

    e.i, for example i created a function that would fetch some content from the server. the function would return the ajax object cause another event listener may be added for it to re-enable the button after the request was accepted.

    hope you got my point. thanks.

  6. #6
    Join Date
    Feb 2008
    Posts
    1,666
    Quote Originally Posted by chronoel_22 View Post
    i was trying to create a library file that would do some common functionality and i would want to return the ajax object cause the caller might want to do something else with it. to do this they would add another event listener to it.
    Well, funny thing is that I did create a set of library functions for passing back the AJAX object and further manipulating it with other functions. You can create your own addListener method on the AJAX object you create. The user can just pass a pointer to their function when you're ready to attach their listener. The two methods I showed are the methods I used to attach/unattach the pointer to the user's function.

  7. #7
    Join Date
    Oct 2008
    Posts
    5
    oh yes very nice. you don't know how hard i've tried to find a workaround. but anyway, i think i'm getting your idea. and if i'm right...it's roughly implemented this way:

    var _listenerFunctions = new Array;

    function addListener(func) {
    _listenerFunction.push(func);
    }

    function listenerFunction() {
    for (i in _listenerFunctions) {
    _listenerFunctions[i];
    }
    }

    function instantiate_ajax_object() {
    // instantiate ajax object of used browser
    ajax_object.onreadystatechange = listenerFunction;

    return ajax_object;
    }

    ajax_object = instantiate_ajax_object();
    addListener(func1);
    addListener(func2);

    is that right?

    and one more thing. how can i attach a function to the ajax_object so that i can do it this way instead:

    ajax_object.addListener(func1);
    ajax_object.addListener(func2);

    ...making addListener a member method of ajax_object.

  8. #8
    Join Date
    Oct 2008
    Posts
    5
    oh yes, i got it. i can create a new class to encapsulate the ajax functionalities. the addListener could be a member method of it now.

  9. #9
    Join Date
    Feb 2008
    Posts
    1,666
    Quote Originally Posted by chronoel_22 View Post
    ... and one more thing. how can i attach a function to the ajax_object so that i can do it this way instead:

    ajax_object.addListener(func1);
    ajax_object.addListener(func2);

    ...making addListener a member method of ajax_object.
    Once you instantiate the object, won't it let you add a new method to it? As follows:
    Code:
    ajax_object = new ActiveXObject("Msxml2.XMLHTTP.7.0");
    ajax_object.addListener = function()
    {
        ... your code ...
        return true;
    }
    Maybe not. Let me know how your efforts turn out. ;-)
    Last edited by MrNobody; 10-30-2008 at 03:32 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles