Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    convert function to string

    I want to convert a function in to a string so i can later display it.
    how would i do it?

    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script>
    
    $(document).ready(function(){ 
    $.ajax({
    	type: "GET",
    	url: "file.php",
    	async: false,
    });
    });
    </script>

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You do that with the function's toString method.

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I searched that method and i did not get it.
    How would you make the above code to a function?
    Thanks in advance!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try using eval():-

    Code:
    <script type = "text/javascript">
    function a () {
    var x = 10;
    var y = 8;
    var z = x+y;
    }
    
    alert (eval(a).toString());
    
    </script>

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Philip, could you please explain what that snippet does? I don't quite get that. As far as I can tell, eval expects a String as parameter, so eval calls the toString method of the function a, evaluates the resulting string and returns the function a. Then you call the toString method of the function a and alert the resulting string.

    Now, what's wrong with just using a.toString()? I don't see how that eval changes anything.

    Also, if the result is only to be alerted, I suppose alert(a) will suffice, since it calls the toString method of a non-String parameter automatically.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by venegal View Post
    Philip, could you please explain what that snippet does? I don't quite get that. As far as I can tell, eval expects a String as parameter, so eval calls the toString method of the function a, evaluates the resulting string and returns the function a. Then you call the toString method of the function a and alert the resulting string.

    Now, what's wrong with just using a.toString()? I don't see how that eval changes anything.

    Also, if the result is only to be alerted, I suppose alert(a) will suffice, since it calls the toString method of a non-String parameter automatically.
    Yes, you are quite right. Must be asleep this morning!

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by 510carlos View Post
    I want to convert a function in to a string so i can later display it.
    how would i do it?

    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script>
    
    $(document).ready(function(){ 
    $.ajax({
    	type: "GET",
    	url: "file.php",
    	async: false,
    });
    });
    </script>
    That is not a function. That is a custom method. It is not quite the same thing. Anyway, to be frank, I don't sense why do you need that. Can you detail your need?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The .toString() worked perfectly!!
    Thank you!

  • #9
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    FYI: Not all browsers support that...

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by A1ien51 View Post
    FYI: Not all browsers support that...

    Eric
    maybe i'm just clueless, but i have no idea what you mean...

    can you please be a little more specific: which browsers don't support what?
    Last edited by rnd me; 07-27-2009 at 02:47 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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