Topic
3 replies Latest Post - ‏2011-10-26T12:53:53Z by SystemAdmin
tensor
tensor
2 Posts
ACCEPTED ANSWER

Pinned topic ajax and and javascript function

‏2011-10-24T19:36:57Z |
Any idea why it doesn't work?

<html> <head> <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" /> <script> function Ajax(url,stdin,stdout) 
{ var xmlHttpReq = 

false; var self1 = 

this; 
// Mozilla/Safari 

if (window.XMLHttpRequest) 
{ xmlHttpReq = 

new XMLHttpRequest(); 
} 
// IE 

else 

if (window.ActiveXObject) 
{ xmlHttpReq = 

new ActiveXObject(
"Microsoft.XMLHTTP"); 
} 

if (!xmlHttpReq) 
{ alert(
'ERROR AJAX:( Cannot create an XMLHTTP instance'); 

return 

false; 
} xmlHttpReq.open(
'POST', url, 

true); xmlHttpReq.setRequestHeader(
'Content-Type', 
'application/x-www-form-urlencoded'); xmlHttpReq.onreadystatechange = function() 
{ 

if (xmlHttpReq.readyState == 4 )document.getElementById(stdout).innerHTML=xmlHttpReq.responseText; 
} xmlHttpReq.send(stdin); 
} </script> </head> <body> <div id=
"aj"></div> <input onclick=
"Ajax('t1.php','','aj')" type=button value=
"ajax"> <input onclick=
"f();" type=button value=
"alert"> </body> </html>

t1.php:

<?php echo 
"<script>function f(){alert('ok')}</script>"; ?>
Updated on 2011-10-26T12:53:53Z at 2011-10-26T12:53:53Z by SystemAdmin
  • seohulu
    seohulu
    4 Posts
    ACCEPTED ANSWER

    Re: ajax and and javascript function

    ‏2011-10-24T21:46:51Z  in response to tensor
    MAYBE ....
    your "script" tag is without "type='text/javascript'"?

    Acoolme is an Online Marketing Software Platform And Social Community
    • tensor
      tensor
      2 Posts
      ACCEPTED ANSWER

      Re: ajax and and javascript function

      ‏2011-10-24T22:26:53Z  in response to seohulu
      Thanks, but still doesn't work. Please check it on your system. No much work.
  • SystemAdmin
    SystemAdmin
    1140 Posts
    ACCEPTED ANSWER

    Re: ajax and and javascript function

    ‏2011-10-26T12:53:53Z  in response to tensor
    This is basically a more difficult combination of givens for dynamic insertion of scripts: that the echo structure with a <script> tag pair wrapping. Particularly, the layout calls for a "plain old" style of client-side script without help of libraries to coverup what now some people would say urgy aspect of the thing.

    This is how I would script the callback.
    
    xmlHttpReq.onreadystatechange = function() 
    { 
    
    if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) 
    { var rid=
    "random89305fixedid";  
    //random but fixed id, purpose is to avoid any possible collision 
    
    if (document.getElementById(rid)) 
    { var dynscript=document.getElementById(rid); dynscript.getParentNode().removeChild(dynscript); 
    } var s=xmlHttpReq.responseText; 
    //alert(s); 
    //Acknowledgement: profitted from a study by Martin Honnen long time ago at faqts.com on some very delicate details of ie browser. 
    //Basically dealing with specific need for ie browser, but gracefully accepted by others too. 
    //This realization is, though, completely mine. var rx=
    
    new RegExp(
    "(<script[^>]*?)(defer\s*(=\s*(\"|')(defer|true)(\\4))?)?([^>]*?>)",
    "ig"); var t=s.replace(rx,
    "$1"+
    " defer=\"defer\" "+
    "$7"); var u=
    "<div id="\
    ""+rid+
    "\"" style=
    "display:none;"><span> </span>
    "+t+"</div>
    "; var o=document.getElementById(stdout); 
    //in any case, exclude firefox and opera here. (opera supports insertAdjacentHTML but works differently and it won't work here.) 
    
    if (window.ActiveXObject) 
    {  
    //do not use conditional on support of insertAdjacentHTML, reason as mentioned. o.insertAdjacentHTML(
    "afterEnd", u); 
    } 
    
    else 
    { 
    //Acknowledgement: profitted from a study on cross-browser support of insertAdjacentXXX by Thor Larholm. var r = o.ownerDocument.createRange(); r.setStartBefore(o); var pcdata=r.createContextualFragment(u); 
    
    if (o.nextSibling) 
    { o.parentNode.insertBefore(pcdata,o.nextSibling); 
    } 
    
    else 
    { o.parentNode.appendChild(pcdata); 
    } 
    } 
    } 
    }