开发者

"Object Expected" error while capturing the page load event

The following code inside the tags gives error: "Object Expected".

    <!-- JQuery/AJAX-->
    <script type="text/javascript">
        try {
            $(document).ready(function(){
                $("p").load(function(){
                    MakeRequest('divElectionCategory','ulElectionCategory','SELECT * FROM ele开发者_运维问答ctioncategorymaster', 'UnOrderedList');
                });
            });
        }
        catch(e)
        {
            alert(e.message);
        }
    </script>

The MakeRequest function resides in a separate .js file and I have included that file before the above given code.

Which object it is referring to?

Edited: The MakeRequest function

function MakeRequest(DivName, ControlName, SqlQuery, ControlType)
{
  var xmlHttp = getXMLHttp();
  var strUrl = "";

  if (ControlType = 'DropDown')
      strUrl = "../phplibraries/filldropdown.php?DivName=" + DivName + "&DropDownControlName=" + ControlName + "&SqlQuery=" + SqlQuery;
  else
      strUrl = "../phplibraries/createelectioncategorymenu.php?DivName=" + DivName + "&ulName=" + ControlName + "&SqlQuery=" + SqlQuery;

  alert(strUrl);
  try 
  {
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4) 
        {
            HandleResponse(xmlHttp.responseText, DivName);
        }
    }
            xmlHttp.open("GET", strUrl, true);
            xmlHttp.send(null);
    }
    catch(err)
    {
        alert(err);
    }
}

I know there is a big security issue above but please ignore it at this point of time.


You cannot call load() that way.

The first parameter of load takes a URL not a function. Perhaps you meant this:

$("p").load( MakeRequest('divElectionCategory','ulElectionCategory','SELECT * FROM electioncategorymaster', 'UnOrderedList') );

That assumes that MakeRequest returns a formatted URL.

EDIT

.load() when used against a DOM element and the first parameter is a function, jQuery assumes you are attaching an event handler. However, p does not have a load event. If you want to wait for everything to load, try this (It doesn't have to be in DOM ready):

$(window).load( function(){
   MakeRequest('divElectionCategory','ulElectionCategory','SELECT * FROM electioncategorymaster', 'UnOrderedList')
});

MakeRequest rewrite

function MakeRequest(DivName, ControlName, SqlQuery, ControlType)
{
  var strUrl = "", params = {};

  if (ControlType = 'DropDown'){
    strUrl = "../phplibraries/filldropdown.php"; 
    params = {
      DivName: DivName,
      DropDownControlName: ControlName,
      SqlQuery: SqlQuery
    }
  } else {
    strUrl = "../phplibraries/createelectioncategorymenu.php";  
    params = {
      DivName: DivName,
      ulName: ControlName,
      SqlQuery: SqlQuery
    }
  }


  alert(strUrl);
  $.get(strUrl, params, function(data){
    HandleResponse(data, DivName);
  });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜