eval function

This section contains a lot of Scripting material for your website.

eval function

Postby shariquekhan on April 16th, 2008, 2:38 pm

Salam tahir bhai javascript is new for me
plz tell how eval function is work?
I know about little bit &
how can we write our own eval() function in javascript
& plz kindly send any javascript reference link
shariquekhan
 
Posts: 8
Joined: April 16th, 2008, 10:34 am

Javascript Eval function

Postby MT Shahzad on April 17th, 2008, 5:41 am

The built-in function eval takes a string as its argument. The string can be is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

If the argument represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements.

This function is useful for evaluating a string representing an arithmetic expression. For example, input from a form element is always a string, but you often want to convert it to a numerical value.

Example
In this example we use eval() on some strings and see what it returns:
Code: Select all
<script type="text/javascript">
<!--
  eval("x=10;y=20;document.write(x*y)");
  document.write("<br />");

  document.write(eval("2+2"));
  document.write("<br />");

  var x=10;
  document.write(eval(x+17));
  document.write("<br />");
-->
</script>


The output of the code above will be:
Code: Select all
200
4
27
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan

Re: eval function

Postby shariquekhan on April 17th, 2008, 1:04 pm

ok I understand but tell me how can I write my own eval function
I have an idea but yeh samajh nahi a raha hai ke javascript mein kon se function use karo
bcoz I'm new in javascript plz help me
shariquekhan
 
Posts: 8
Joined: April 16th, 2008, 10:34 am

Re: eval function

Postby adilibrahim121 on April 17th, 2008, 5:20 pm

w.sallam shariq bhai...App ka question asal may hamein samajh nahi aa raha k app kehna kia chahtay hain....ap apna eval funtion kesay declare karna chahtay hain please mention a detail note about it ok...anyways attachment may aik eval funtion use kia gaya hay and it is a very very basic sort of example app check karein shayad kuch help mil jaye iss say...and most of all apni problems ham say share karnay may koi hasitation feel nahi karna okkzz....With regards
ADIL IBRAHIM
You do not have the required permissions to view the files attached to this post.
User avatar
adilibrahim121
 
Posts: 13
Joined: March 14th, 2008, 4:15 pm
Location: Peshawar

Re: eval function

Postby shariquekhan on April 18th, 2008, 10:19 am

mujhe eval ka function use kerna to samajh a gaya hai balke mai ne use bhi ker ke dekh lia hai ap mujhe yeh batao ke mai javascript use kerte hue ek apna function write kerna chah raha hon jis ko mai yeh string do
eval("2+2")
to wo mujhe is ka result return kare 4.
shariquekhan
 
Posts: 8
Joined: April 16th, 2008, 10:34 am

Re: eval function

Postby MT Shahzad on April 23rd, 2008, 10:49 am

Hello!

The thing you are talking about is might not be possible as you will not be able to parse each command. However you can write a simple function with basic operations that you will be able to perform after parsing the string, but this also will be not a piece of cake, you might have to implement low level programming concepts.
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan


Return to Java Script / VB Script

Who is online

Users browsing this forum: No registered users and 0 guests