View Full Version : Copying Javascript routines from one window to another

01-16-2004, 08:43 PM
ok. Here's what I want to do....

I don't want to use a common .js file, although
that may be the easiest way out.

I'm designing a front-end page that will
enter a month and year, then it will open
a new window, produce a table by writing
HTML line by line for each day of the month.

No problem.

Now I also want to imbed some javascript
routines into that window. Aside from storing
the routines in an array in the front end page
and writing the array line by line, is there anyway
to "read" the javascript from the front end and
write them to the new window?

The front end doesn't use most of these routines, it's just
being a carrier to put them in the new window.
however, there are going to be some routines that
will be used in both.

I don't want the new window to reference the
routines from the front end window, because once
the new window is created, it's meant to be saved
to the hard drive, then clicked on locally and updated
then saved again.

The front end is only to produce the appropriate
table cells for this window.

I'd also like the window to have the ability to create
a new window based on the next month, and carry
over the data to start off the new window.

Does this make any sense?

Basically, I'm making one static HTML page, which will
dynamically create a new HTML page which can be saved
and later manipulated without losing entered data.

and later that dynamically created page, once it's month's
worth of data has been entered, will have the ability to
spawn off a new dynamically created page, carrying over
ending totals from the previous as a starting total in the new.


01-16-2004, 09:03 PM
There's ways. It's far easier if you either export them to the other window from the document they are in, but you can do it the other way as well. You can also execute them in the first window from the other, if you wish. The procedure to move them would be something like this:
// In the first window, copy to the other
[object OtherWindow].window[[string FunctionName]] = [function FunctionName];

// In the other window, copy from the first window
var fnFunctionName = [object FirstWindow].window[[string FunctionName]];

// In the other window, execute in the first window
[object FirstWindow].window[[string FunctionName]]( arguments )How you access the first window from the second depends on how they are related. A popup would use window.opener, while a frame would use window.parent, or window.top. Windows that are not related can be made related by using the window.open function with their window name as one of the arguments, to create a popup relation. Note that this does not reliably work for frames.

01-16-2004, 09:07 PM
I can't execute them from the other window. Once the
new window is created and saved to the hard drive.

The next day/week, etc, only the new window will be
available (locally), not off the web.

So any/all javascript routines MUST be transferred over
to the new window, so they can be saved with it.

Almost like I need to have the routines stored as text
in an array for transferring.

Is there a way to "read" the javascript using javascript.
That is, be able to read all javascript functions on a page
and write them to another window

Like you can read the HTML of a page using elements[],
but can you read javascript of a page?

01-16-2004, 09:12 PM
Have you tried to alert a function - without the parentheses - some time? That's your way to read the function as text.

01-16-2004, 09:13 PM
I don't get it...explain..


01-16-2004, 09:17 PM
Try it:
alert([function YourFunction]);See that the function is written out when it gets converted to a string? You can do the same conversion using
var sYourFunctionSource = [function YourFunction].toString();

01-16-2004, 09:29 PM
is the function still usabe? or just displayable? when it's
referenced that way.

That looks like it could get real ugly, if it were say a 50 line
function for instance.

I'll have to toy with that to see what it does, right now, I
can't picture it in my mind yet.


01-16-2004, 09:33 PM
It's still usable (actually that is the form the function is represented as internally) but you have to be wary when it comes to scopes and variables. Also note that this does not work for closures, only for source functions. A closure must be reinstanciated from the original function.