Solved

mm_calljs function (performs an eval on an onClick)

Posted on 2004-08-30
4
1,700 Views
Last Modified: 2008-01-09
Hi,
I'm fixing up someones code and came across this function:

function mm_calljs(jsStr) { //v2.0
  return eval(jsStr);
}

It's called by the following:

<a href="help.htm#javascript_required" onclick="mm_calljs('javascript:small()'); return false;">

I can't find any reason for passing it through this function.  I'm thinking it could be for backwards capability.

I'm assuming it came from a macromedia editor.

What I'm looking for is a reason for why it is put there, and which piece of software/version it came from?

Thanks, Alexi.
0
Comment
Question by:-Alexi-
  • 3
4 Comments
 
LVL 33

Expert Comment

by:sajuks
Comment Utility
the code does come from MacroMedia's DreamWeaver Editor.

function mm_calljs(jsStr) { //v2.0
  return eval(jsStr);
}
The real power of the eval command is its ability to work with data that is not known at load time—for example, user-entered data.  EVAL takes any string that you provide and executes it returning a result, if one is applicable. You can use this to execute JavaScript where what you are being asked to execute is not known at load time
If we take the below example:

function MM_jumpMenu(targ,selObj,restore)
{
    EVAL(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}


This code sets the location of the object specified by the targ parameter to that of the value attribute of the selObj drop-down; the other parameter simply resets the drop-down if set to true. The reason that EVAL is used here is that it allows the developer to provide the target for the function, which could be a variety of things, for example:

targ value - load the selected URL
Document - current window
Parent - parent document when using Frames/IFrames
newwin - A different window identified by newwin
parent.mainframe - a sibling Frame/Iframe identified by mainframe
Top - The top level item in the current window


0
 
LVL 33

Expert Comment

by:sajuks
Comment Utility
In your case it is evaluating the output that is generated from the function small() .
0
 

Author Comment

by:-Alexi-
Comment Utility
thanks, but small doesn't return anything and there is no user defined variables.  Is there any advantage to passing it through the mm_calljs function? as opposed to:

onClick="small(); return false;"

I can't see any immediate advantage, but because it has to comply with accessibility guidelines I guessed it might be for backwards compatibility.
0
 
LVL 33

Accepted Solution

by:
sajuks earned 251 total points
Comment Utility
If small() doesnt have anything then wy do you still want to call it ?Can you post your function small() ?
The advantages of using EVAl are as stated above and if not required then there is really no reason why it needs to be called.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now