Javascript split function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DannyMc
    New Member
    • Aug 2007
    • 20

    Javascript split function

    Hi, i am currently having problem with the split function. Below are the code:

    Code:
    function callback_hello(str) {
           var str_b4_split = (str);
    	   var str_af_split = str_b4_split.split(" ");
    	  document.write(str_af_split);
    }
    callback_hello will receive a return string value and store in str_b4_split.

    I tried to display str_b4_split , and it is successful using document.write( str_b4_split).

    However, when i tried to add in the split, i was unable to display any of it in IE7, and firefox 2.0. Firefox shown nothing, IE7 shown "Object doesn't support this property or method" in error msg.


    May i know what is the reason behind this? thank you
  • Ranjan kumar Barik
    New Member
    • Aug 2007
    • 95

    #2
    Originally posted by DannyMc
    Hi, i am currently having problem with the split function. Below are the code:

    Code:
    function callback_hello(str) {
           var str_b4_split = (str);
    	   var str_af_split = str_b4_split.split(" ");
    	  document.write(str_af_split);
    }
    callback_hello will receive a return string value and store in str_b4_split.

    I tried to display str_b4_split , and it is successful using document.write( str_b4_split).

    However, when i tried to add in the split, i was unable to display any of it in IE7, and firefox 2.0. Firefox shown nothing, IE7 shown "Object doesn't support this property or method" in error msg.


    May i know what is the reason behind this? thank you
    Hi,

    I have tried your code in firefox 2.0 but it's working fine.
    I donot have IE7 but Idonot think that will create any problem.
    By the way just check if you have embeded the javascript codes inside script tags properly and also called it properly.

    Happy Programming!

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      hi ...

      please post more of your code especially where you call your function ... may be str is undefined at the moment where the function is called?

      kind regards

      Comment

      • DannyMc
        New Member
        • Aug 2007
        • 20

        #4
        Hi, I am using AjaxAgent from ajaxagent.org. I have modified the example code given in http://ajaxagent.org/doc.php . I will not attach agent.php in here(You will need it to run the code below).

        Code:
        <?php 
          // server side function
          function getdetail()
        {
        exec("ping www.google.com", $coutput);
        return $coutput;
        }
        ?>
        
        <?php 
          // STEP 1: INCLUDING AJAX AGENT FRAMEWORK/LIBRARY
          include_once("agent.php");
        ?>
        
        <html>
        <head>
        <title>My Ajax Agent Test Page</title>
        <?php
          // STEP 2: INITIALIZING SERVER AGENT TO CREATE CLIENT AGENT
          $agent->init(); 
        ?>
        </head>
        <body>
          <script language="JavaScript">
        
            // client function  
            function call_hello() {
              // STEP 3: CALLING SERVER FUNCTION FROM CLIENT AGENT
              agent.call('','getdetail','callback_hello');
            }
            // client callback function
            function callback_hello(str) {
               var myString = (str);
              document.write(myString);
        }
        
           
          </script>
        <p>testing</p>
        <a href="javascript:call_hello();">Test the hello function</a>
        
        </body>
        </html>
        Here is the result i will get:
        ,Pinging www.l.google.co m [72.14.253.99] with 32 bytes of data:,,Reply from 72.14.253.99: bytes=32 time=197ms TTL=236,Reply from 72.14.253.99: bytes=32 time=198ms TTL=236,Reply from 72.14.253.99: bytes=32 time=197ms TTL=236,Reply from 72.14.253.99: bytes=32 time=198ms TTL=236,,Ping statistics for 72.14.253.99:, Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),,Approxim ate round trip times in milli-seconds:, Minimum = 197ms, Maximum = 198ms, Average = 197ms


        However, after i split the myString:
        Code:
        <?php 
          // server side function
          function getdetail()
        {
        exec("ping www.google.com", $coutput);
        return $coutput;
        }
        ?>
        
        <?php 
          // STEP 1: INCLUDING AJAX AGENT FRAMEWORK/LIBRARY
          include_once("agent.php");
        ?>
        
        <html>
        <head>
        <title>My Ajax Agent Test Page</title>
        <?php
          // STEP 2: INITIALIZING SERVER AGENT TO CREATE CLIENT AGENT
          $agent->init(); 
        ?>
        </head>
        <body>
          <script language="JavaScript">
        
            // client function  
            function call_hello() {
              // STEP 3: CALLING SERVER FUNCTION FROM CLIENT AGENT
              agent.call('','getdetail','callback_hello');
            }
            // client callback function
            function callback_hello(str) {
               var myString = (str);
               var mySplitResult = myString.split(",");  //--------------->HERE
              document.write(mySplitResult); // ------------------------------->HERE
        }
        
           
          </script>
        <p>testing</p>
        <a href="javascript:call_hello();">Test the hello function</a>
        
        </body>
        </html>

        I was unable to get anything. Is there anything wrong with the code?

        I have even tried with var mySplitResult = myString.split( "\\,"); and still not working. Please advice. thank you.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          so i assume that:

          Code:
          ',Pinging www.l.google.com [72.14.253.99] with 32 bytes of data:,,Reply from 72.14.253.99: bytes=32 time=197ms TTL=236,Reply from 72.14.253.99: bytes=32 time=198ms TTL=236,Reply from 72.14.253.99: bytes=32 time=197ms TTL=236,Reply from 72.14.253.99: bytes=32 time=198ms TTL=236,,Ping statistics for 72.14.253.99:, Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),,Approximate round trip times in milli-seconds:, Minimum = 197ms, Maximum = 198ms, Average = 197ms'
          is the result that you get as param str in your callback-function? could you alert it before the split-operation?

          kind regards

          Comment

          Working...