...

View Full Version : convert function to string



510carlos
07-26-2009, 12:18 AM
I want to convert a function in to a string so i can later display it.
how would i do it?


<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>

venegal
07-26-2009, 12:32 AM
You do that with the function's toString method.

510carlos
07-26-2009, 06:58 AM
I searched that method and i did not get it.
How would you make the above code to a function?
Thanks in advance!

Philip M
07-26-2009, 10:12 AM
Try using eval():-


<script type = "text/javascript">
function a () {
var x = 10;
var y = 8;
var z = x+y;
}

alert (eval(a).toString());

</script>

venegal
07-26-2009, 01:03 PM
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.

Philip M
07-26-2009, 01:56 PM
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! :)

Kor
07-26-2009, 04:09 PM
I want to convert a function in to a string so i can later display it.
how would i do it?


<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?

510carlos
07-26-2009, 06:02 PM
The .toString() worked perfectly!!
Thank you!

A1ien51
07-27-2009, 01:02 AM
FYI: Not all browsers support that...

Eric

rnd me
07-27-2009, 03:42 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum