SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Location
    Chennai
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How To Execute A page contain javascript function in AJAX Response?

    Hi,

    How To Execute A page contain javascript function in AJAX Response?

    Ajax Response
    <script type="text/javascript">
    function muthu(){
    alert('hai');
    }
    </script>
    <script type="text/javascript">
    function muthu123(){
    alert('hai123');
    }
    </script>
    <script type="text/javascript">
    function muthu1234(){
    alert('hai1234');
    }
    </script>



    MuthuRaja

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You could use innerHTML to place it somewhere within the page. The scripts should be accessible then.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Location
    Chennai
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    Its not Working.

    My indexpage.html code here

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head></head>
    <body>
    <div id="show"></div>
    <input type="button" value="makeReq" onClick="makeReq()" />
    <input type="button" value="muthu" onClick="muthufn()" />qwer
    <script>
    function makeReq(){
    mR('getScript.html',cFn,'');
    }
    mR=function(a,b,c,d){d=this.ActiveXObject;d=new(d?d:XMLHttpRequest)('Microsoft.XMLHTTP');d.open(c?'POST':'GET',a,1);d.setRequestHeader('Content-type','application/x-www-form-urlencoded');d.onreadystatechange=function(){d.readyState>3&&b?b(d.responseText,d):0};d.send(c)}

    function cFn(res){
    document.getElementById('show').innerHTML=res;
    eval(document.getElementById('show'));
    }
    </script>
    </body>
    </html>

    getScript.html Code Here
    <script type="text/javascript">
    function muthufn(){
    alert('hai');
    }
    </script>
    After I click muthu button, muthufn is not defined error throw.


    MuthuRaja

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    By default javascript through Ajax is treated as a string like all other data been passed unless given a method type to be parsed as. eval() which isn't the best thing to use anymore will activate any javascript source that is dynamically added to the page unless you run an interval until the source is active.


Bookmarks

Posting Permissions

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