Advertisement

04.05.2008 at 01:42AM PDT, ID: 23298090
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Tricky: Clone function
Zone: JavaScript
Tags: script, clone, freed
Hi,
I have a frameset with a function in frame2 that I want to save and use later before I change the page in frame2.

So I did this:
Imagine the function names are
test0, test1 and test2

and a typical example is

function test0() {
  openWin('bla.html');
  top.menuFrame.location='bla1.html'
}

var functionCopies = new Array();
for (var i=0;i<3;i++) {
  var f = 'top.otherFrame.test'+i;
  functionCopies[functionCopies.length] = eval(f);
}

HOWEVER when I tried to execute
functionCopies[1]();

I get a VERY weird error in firefox which is pointing to a completely wrong line in the new page
For once IE gave me a much more significant error: Cannot access a freed function
So I realised that the copy of the code was not a copy but a pointer to a function that no longer existed

Actually writing this now, I believe that the function IS copied correctly but the openWin no longer exists when I try to execute it later.

Can you
a) confirm that my copy method is ok and that I need to make sure I do NOT call another function in the original script and if that is the case how can I copy that code too?
b) if needed fix the attached script from irt (http://www.irt.org/script/1234.htm) which gives unterminated string in IE only in the eval? (I tried unescape)

Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
<html>
 
<head>
<script language="JavaScript"><!--
function testwithparm( _val1, _val2 )
{
 alert( 'val1=' + _val1 );
 alert( "val2=" + _val2 );
}
 
function testnoparm()
{
 alert("Hello World!");
 alert("I have no parameters!");  
}
 
function cloneFunctionToString( _func )
{
 // Find first the whole function string:
 var functionString = _func.toString();
  
 // Extract the function body:
 var bodyString     = functionString.substring( functionString.indexOf("{")+1, functionString.lastIndexOf("}") ); 
   
 // Extract the function argument:  
 var functionArg = functionString.substring( functionString.indexOf("(")+1, functionString.indexOf(")") );
 
 // Build an array containing all function arguments:
 var argArray = functionArg.split(",");
  
 // Start building the cloning string:
 var retString = "new Function(";
  
 // Add paramters to cloning string:
 for ( var i = 0; i < argArray.length; i++ )
 {
  retString += "'" + argArray[i] + "',";
 }  
 
 // Add function body to cloning string:
 retString += "'" + bodyString + "')";
  
 // The function body contains unwanted newlines, remove them:
 retString = retString.replace(/\n/g," ");
 return retString;
} 
 
// Example: Function with parameters cloned:
var testwithparmCloned = eval( cloneFunctionToString( testwithparm ) );
 
// Example: Function without parameters cloned:
var testnoparmCloned = eval( cloneFunctionToString( testnoparm ) );
 
//--></script>
</head>
 
<body onLoad="testwithparmCloned('Hello','World'); testnoparmCloned();">
</body>
 
</html>
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: mplungjan
Solution Provided By: RQuadling
Participating Experts: 1
Solution Grade: B
Views: 0
Translate:
Loading Advertisement...
04.05.2008 at 03:00AM PDT, ID: 21287601

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 03:06AM PDT, ID: 21287607

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 03:15AM PDT, ID: 21287620

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 03:19AM PDT, ID: 21287631

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 05:36AM PDT, ID: 21287912

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 05:38AM PDT, ID: 21287914

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 06:40AM PDT, ID: 21288146

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 09:22AM PDT, ID: 21288635

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 11:11AM PDT, ID: 21289044

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 12:25PM PDT, ID: 21289295

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.05.2008 at 03:00AM PDT, ID: 21287601

Rank: Master

Hmm.

Good one!

The single quotes need escaping.

Using IE8's debugger ...

// Example: Function with parameters cloned:
var s_ClonedWith = cloneFunctionToString( testwithparm );
var testwithparmCloned = eval(s_ClonedWith);


This is what s_ClonedWith contains in the debugger ...

"new Function(' _val1',' _val2 ','alert( 'val1=' + _val1 );alert( \"val2=\" + _val2 );')"

So, the when this comes out of "" you get ...

new Function(' _val1',' _val2 ','alert( 'val1=' + _val1 );alert( "val2=" + _val2 );')

The third param of new Function is ...

alert( ' and the ' close's it.

So try using all " or add a search replace of ' into \'

Obviously, any escaped quote will need to be double escaped. This gets messy.

By this I mean, say your function contains ...

alert('This isn\'t too hard!');

I'm not even going to try and work out what you need to do.

Using " though does solve the problem.
 
04.05.2008 at 03:06AM PDT, ID: 21287607

Rank: Master

Ah.

Even with double quotes, all ' will still need to be escaped.

 // Add function body to cloning string:
 retString += "'" + bodyString.replace(/'/g, "\\'") + "')";
 
 // The function body contains unwanted newlines, remove them:
 retString = retString.replace(/[\t\n\r]+/g," ");


seem to help.

Now testing an escaped ' and " in the functions ...
 
04.05.2008 at 03:15AM PDT, ID: 21287620

Rank: Master

Using the search\replace above and this call in the function ...

      alert("This isn't too hard!");


It is OK.

But with this in the function ...

      alert("He said \"This should be a piece of cake!\".");

And now I'm having trouble keeping track.

The " and \ all need to be escaped.

So ...

 
04.05.2008 at 03:19AM PDT, ID: 21287631

Rank: Master

Oh and look at me! I'm a genius!!!!

 // Add function body to cloning string:
 retString += "'" + bodyString.replace(/(['"\\])/g, "\\$1") + "')";


This seems to be working fine in IE8 and FF3.0b5

Oh look at on the cutting edge!
Accepted Solution
 
04.05.2008 at 05:36AM PDT, ID: 21287912
Okeee (although cutting edge is maybe not the word to use when fixing a script from 1999 or so ;)

Here is an alternative way to define the actual function (comment to the original script)
http://www.irt.org/feedback/2107.htm


var newFunc = function (arg1, arg2, '{jscript stuff}'); //The method used in your knowledge base

var newFunc = function (arg1, arg2){jscript stuff}; //seems to be much less fussy about contents of the function being cloned.

which might mean your quotes are no longer needed...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function cloneFunction( funcName )
{
// Find first the whole function string. This can even be in another frame:
eval (" var functionString = " + funcName + ".toString();");
 
// Extract the function body - everything from the first "(" to the end :
var bodyString = functionString.subst( functionString.indexOf("("));
 
// Add declaration bits and return. Note name of new function will be funcNameCloned:
return "var " + funcName + "Cloned = function " + bodyString;
} 
Open in New Window
 
04.05.2008 at 05:38AM PDT, ID: 21287914
I will make a quick test to see if our discussion is completely irrelevant...
 
04.05.2008 at 06:40AM PDT, ID: 21288146
Ok, here is the test:
http://www.plungjan.name/test/cloneacrossframes/

Please scroll down

Then explain why the second set does not work and how to get the first set to work after changing page - it works in Mozilla but NOT in FF/IE if you first copy, then execute then change page then execute

Michel
 
04.05.2008 at 09:22AM PDT, ID: 21288635

Rank: Master

What is a valid use case for this? Why would you clone a function across frames when loading the js from the server is so much easier.

You won't be able to copy across frames for different domains/addresses.

What is a function prototype? Maybe you can copy a function's properties?

 
04.05.2008 at 11:11AM PDT, ID: 21289044
1. same domain - I know about cross domain issues
2. I am saving dynamically generated functions containing data from a database  to be used later while allowing the user to load other pages with other functions containing other data...

I think I will extract the data from the function instead - I am pretty sure my trouble is calling an unloaded function from the saved function
 
04.05.2008 at 12:25PM PDT, ID: 21289295

Rank: Master

Rather than hold it in a frame, let the frameset hold the function?

That way the parent to all frames hold the data and all frames can access it.

Therefore making the frames display only sort of thing rather than making the requests to the server (I assume this is all AJAX).
 
 
20080236-EE-VQP-29 / EE_QW_EXPERT_20070906