开发者

How do I Call Ajax for the first time after page loads?

here is my code:

  ...
    <script type="text/ja开发者_StackOverflowvascript">
function ajaxcall(div, page)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(div).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",page,true);
xmlhttp.send();
}
</script>
</head>
<table border="0" cellpadding="0" cellspacing="0">
 <tr>

  <td width="309" valign="top">

  <div width="309" height="270" id="menu"></div>
   <script type="text/javascript">ajaxcall('menu', 'map2.php')</script>
....

i want when the page loads it does the ajax and attach to the div "menu! the map2.php but it doesnt, what im doing wrong?


Things to check:

Its the DOM you are using available when the script is called?

The function has to be called in order to do the request. And more importantly the dom elements used in the function must exist. To make sure the node exists place the your function call after the div has closed in an inline script tag, or attach an event handler to window load event or use a js framework domready/domloaded/ready ( same thing different names ) event

Its the request being sent? Use Firefox with Firebug extension to fugure it out

Are they any error/exceptions not allowing the js run? Use firebug to check for errors

Hope this helps

PS: xhhtrequest.onerror should be called in the event of a bad request, if you cannot use firebug attach a function to alert for errors

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜