Doesn't execute Javascript when called using ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perdeepOsingh
    New Member
    • Aug 2008
    • 8

    Doesn't execute Javascript when called using ajax

    Hi,
    What I am trying here is that from my first file I am making a call to handler.php file
    //////////////////////////////FirstFile////////////////////////
    [HTML]<html>
    <head>
    <script src='jquery-1.1.3.1.pack.js ' type='text/javascript'></script>
    <script type='text/javascript'>
    $.post("http://practice.dev/handler.php",
    function(data)
    {
    alert(data);
    //document.write( data);
    $("#test").appe nd(data);
    $("#test").html (data);
    });
    </script>
    </head>
    <body>
    <div id='test'>
    </div>
    </body>
    </html>[/HTML]

    ////////////////////////handler.php/////////////////
    [CODE=php]<?php
    echo 'here';
    ?>[/CODE]
    [CODE=javascript]<script type='text/javascript'>
    alert('hello');
    </script>
    [/CODE]////////////////////////////////////////////////////////////

    Now in firefox it is working fine means it show's alert saying hello, but when I try in IE it doesn't but if in callback function instead of using append/prepend/ html, if I use document.write( data) then it runs the javascript means it display hello message in IE 7.

    Anything I am doing wrong ? IE sucks plz help help help ?
    Last edited by gits; Aug 22 '08, 07:57 AM. Reason: added code tags
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Just run that file with the help of a server. It will work fine. In IE the functionslitite s are written like that. Place your code within a server and run it, it will work.

    Regards
    Ramanan Kalirajan

    Comment

    • perdeepOsingh
      New Member
      • Aug 2008
      • 8

      #3
      Thanks for quick reply

      Run from server ? Yes, I am running it on apache server. sure there is some other catch to it.

      help help

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Originally posted by perdeepOsingh
        Thanks for quick reply

        Run from server ? Yes, I am running it on apache server. sure there is some other catch to it.

        help help
        Instead of append just try using insert, or update. I am not very familiar about this. Just trial and error method. this is my suggestion not the solution.

        Regards
        Ramanan Kalirajan

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          are both files, the html and the php on the same server and do you test from there, or from a local file?

          Comment

          • perdeepOsingh
            New Member
            • Aug 2008
            • 8

            #6
            Originally posted by gits
            are both files, the html and the php on the same server and do you test from there, or from a local file?
            both files on same server: Yes,
            Test from server: Yes

            Plz help help ...

            Comment

            • RamananKalirajan
              Contributor
              • Mar 2008
              • 608

              #7
              Originally posted by perdeepOsingh
              both files on same server: Yes,
              Test from server: Yes

              Plz help help ...
              Hi, wether you are using Prototype Js Framework

              [HTML]$.post("http://practice.dev/handler.php",
              function(data)
              {
              alert(data);
              //document.write( data);
              $("#test").appe nd(data);
              $("#test").html (data);
              });[/HTML]

              This code seems to be jQuery. I am confused with the URl u had given. I think the problem in ur code may be with the URL ("http://practice.dev/handler.php"). Just check that once. Just visit this site tio know more www.jquery.com

              Regards
              Ramanan Kalirajan

              Comment

              • perdeepOsingh
                New Member
                • Aug 2008
                • 8

                #8
                Originally posted by RamananKaliraja n
                This code seems to be jQuery. I am confused with the URl u had given. I think the problem in ur code may be with the URL ("http://practice.dev/handler.php"). Just check that once. Just visit this site tio know more www.jquery.com

                Regards
                Ramanan Kalirajan
                Hi Ramanan Kalirajan,
                Thanks for reply. Yes it is jquery call [not sure but my understanding is jquery is AJAX correct me if I am wrong.]. Well URL is right and it's my local server virtual host. And yes it's returning the result from handler.php, if we concentrate on my first post with question the only problem is how IE handle javascript in firefox it's working it's executing the javascript written in handler.php but in IE it only works if I use document.write( data).

                Comment

                • RamananKalirajan
                  Contributor
                  • Mar 2008
                  • 608

                  #9
                  Originally posted by perdeepOsingh
                  Hi Ramanan Kalirajan,
                  Thanks for reply. Yes it is jquery call [not sure but my understanding is jquery is AJAX correct me if I am wrong.]. Well URL is right and it's my local server virtual host. And yes it's returning the result from handler.php, if we concentrate on my first post with question the only problem is how IE handle javascript in firefox it's working it's executing the javascript written in handler.php but in IE it only works if I use document.write( data).
                  See what u are saying I am agreeing that. But the basic thing u must understand that each browser has their own implementation logic. This we call as Browser Compatibility problem. i am working in Prototype JS Framework.

                  in IE, $('myBut').read Attribute('valu e') --> this will be working but the same doesn't works on Mozilla.

                  were as the code $('myBut').'val ue will work on both the IE and Mozilla. You have to find out the place were u had gone wrong. For me, it seems to be in the URl part u have specified http://... as it was a localhost u have to give the project workpath or some original path were the file actually placed it will work. Just try it out. i am new to JQuery.

                  Regards
                  Ramanan Kalirajan

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    To run JavaScript, you'll need to eval it or add it to the page via script elements.

                    In jQuery, you'll probably have to use the $.ajax function instead of the simplified $.post function.

                    PS. Don't use document.write after the page has loaded - it should only be used during page load.

                    PPS. jQuery is not only for Ajax.

                    Comment

                    • perdeepOsingh
                      New Member
                      • Aug 2008
                      • 8

                      #11
                      Originally posted by acoder
                      To run JavaScript, you'll need to eval it or add it to the page via script elements.

                      In jQuery, you'll probably have to use the $.ajax function instead of the simplified $.post function.

                      PS. Don't use document.write after the page has loaded - it should only be used during page load.

                      PPS. jQuery is not only for Ajax.
                      Thanks acoder, you nailed it. I was researching it and found exactly what you saying. Yes document.write will screw things up as I will lose current content of my page. True jQuery is so much more like design and other stuff. Ok now I got this idea that i have to eval(data) but now i got new problem. the content of data is something like somehtml.....mo rehtml .............no w <script type='....>aler t('hello')...</script>.....som emorehtml.

                      now if data is just javascript than eval is working but as I have html + javascript any suggestion how to tackle this.

                      Thanks

                      Comment

                      • perdeepOsingh
                        New Member
                        • Aug 2008
                        • 8

                        #12
                        Originally posted by RamananKaliraja n
                        See what u are saying I am agreeing that. But the basic thing u must understand that each browser has their own implementation logic. This we call as Browser Compatibility problem. i am working in Prototype JS Framework.

                        in IE, $('myBut').read Attribute('valu e') --> this will be working but the same doesn't works on Mozilla.

                        were as the code $('myBut').'val ue will work on both the IE and Mozilla. You have to find out the place were u had gone wrong. For me, it seems to be in the URl part u have specified http://... as it was a localhost u have to give the project workpath or some original path were the file actually placed it will work. Just try it out. i am new to JQuery.

                        Regards
                        Ramanan Kalirajan
                        Hi Ramanan,
                        Thanks mate for explaining the browser compatibility. I appreciate it, but i think acoder is making more sense of my problem. Might I didn't managed to explain you my prob. I appreciate your help and effort. Still you can help me but in the direction acoder and me are going.

                        Thanks

                        Comment

                        • RamananKalirajan
                          Contributor
                          • Mar 2008
                          • 608

                          #13
                          Originally posted by perdeepOsingh
                          Hi Ramanan,
                          Thanks mate for explaining the browser compatibility. I appreciate it, but i think acoder is making more sense of my problem. Might I didn't managed to explain you my prob. I appreciate your help and effort. Still you can help me but in the direction acoder and me are going.

                          Thanks
                          Sure, I will be there in the forum trying to help u out.

                          Regards
                          Ramanan Kalirajan

                          Comment

                          • perdeepOsingh
                            New Member
                            • Aug 2008
                            • 8

                            #14
                            Thanks everyone for helping me on this topic. I figured it out and come up with solution
                            Code:
                            if($.browser.msie)
                            	{
                            		try{
                            			var re =/((<[\s\/]*script\b[^>]*>)([^>]*)(<\/script>))/i;
                            			var match;
                            			if(match = re.exec(data)) {
                            					eval(match[3]);
                            			}
                            		}
                            		catch(error){
                            			alert(error);
                            		}
                            	}

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              The reg. exp. looks OK, but why are you using browser detection?

                              Comment

                              Working...