help me !

Discussion in 'JavaScript' started by xxpniu, Jul 6, 2007.

  1. #1
    I write this code For my web file simply.
    but this have one error(bug) in my js file.
    this is the js file ;


    //----------------------------------------------------------
    //get Element by id

    function getEl(id){
    return document.getElementById(id);
    }

    //create element by tag name

    function createEl(tag){
    return document.createElement(tag);
    }

    //change one object's class

    function changeCss(obj,css){
    obj.className=css;
    }

    //create ajax object

    function createAjax(){
    var xmlHttp=null;
    try{
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e){
    // Internet Explorer
    try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp;
    }
    //Ajax Object
    function Ajax(div,url){
    this.ajax = createAjax();//Create ajax object (xmlhtmlrequest object)
    this.url = url; //request url
    this.div = getEl(div); //display response message div
    this.send = function(){
    //ajax=this.ajax;
    this.ajax.open("GET",this.url,true);
    this.ajax.onreadystatechange=this.ajaxOk;
    this.ajax.send(null);
    };//send request
    this.ajaxOk = function(){
    alert(typeof(this.ajax)); // error at this line
    if(this.ajax.readyState == 4 && this.ajax.status == 200)
    this.div.innerHTML=this.ajax.responseText;
    };//get message and display it
    }
    //----------------------------------------------------------------



    then i use it with this code

    //-------------------------------------------------------------------

    function viewMessageList(action){
    var url="NoticeList.asp";
    var div="bodyMain";
    url+="?Action="+action+"&t="+new Date().getTime();
    var ajaxObj=new Ajax(div,url);
    ajaxObj.send();
    }
    //----------------------------------------------------------------------


    but It not do i want , it show this "undefine". someone can help!
    thinks!
     
    xxpniu, Jul 6, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    any insight as to what is undefined?
     
    ansi, Jul 6, 2007 IP
  3. xxpniu

    xxpniu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this.ajaxOk = function(){
    alert(typeof(this.ajax)); //!!******* error at this line ,the line code show undefine `
    if(this.ajax.readyState == 4 && this.ajax.status == 200)
    this.div.innerHTML=this.ajax.responseText;
    };//get message and display it
    }
     
    xxpniu, Jul 6, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    try removing all of the "this." crap...

    ajax = createAjax();
    alert(typeof(ajax));
     
    ansi, Jul 6, 2007 IP
  5. xxpniu

    xxpniu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    remove 'this.' not right
     
    xxpniu, Jul 6, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    says who? your code is freaking hideous...
    i would suggest actually learning javascript before messing with xmlhttp.
     
    ansi, Jul 6, 2007 IP
  7. xxpniu

    xxpniu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    SORRY! I am chinese,My English very poor!
    think you !
    I will study javascript hard.
     
    xxpniu, Jul 8, 2007 IP