Calling javascript function from form result
So I have a html page (actually generated with php) that will create a popup that contains the form. All my javascript files are in a separate javascript file. I want to know if it is possible to call a function from my javascript file from the resulttext form.
Here is the first html page
<script type="text/javascript" src="x.js"></script><h1>hello</h1>
<div id='form'>
<a href='#' onclick="makewindows('<form action="process.php" method="post" enctype="multipart/form-data "><label for="file">Filename:</label><input type="file" name="file" id="file"/> <br /><input type="submit" name="submit" value="Submit" onclick="" /></form>'); return false;">
click here</a>
</div>
After loading the file, a result text is generated that shows the name of the file. In x.js, I have a function to update a div in the original window. SO is it possible to call this from a normal html page?
Here is the code for maewindows
function makewindows(html){
child1 = window.open ("about:blank");
child1.document.write(html);
child1.document.close();
}
I don't want to call the function using onclick, I want to call it from the result text of the form.
a source to share
Do you want to access a Javascript function declared on the same page that opened the window using window.open from a script running in the opened window?
window.opener is what you are looking for.
For example, if you have a function defined x.js
as function foobar(s)
(and x.js
included in the main window), you can return something like this in response from process.php
:
<script type="text/javascript">
window.opener.foobar('returned text');
window.close();
</script>
And the string "returned text" is returned to the function foobar
.
The reason for this is that anything that is defined with a keyword function
from the global scope is automatically added as a property of the current object window
. If your function is not defined in the global scope, you can force it to be a property window
like this:
window.foobar = function(s) { ... }
a source to share
Tobias is right.
But without the opener property, the main window and the target window cannot exchange - no communication.
This dated article illustrates one solution for onclicks, albeit with purposes. You may be interested in playing. http://homepage.mac.com/bigeuniverse/tests/targetwindows/
Better today, however, to do this with listeners and the functions associated with them. I would clearly define the opener in your makewindows function and also define the window.
PARENTS: Like this:
var child1 = null;
function makewindows(html)
{
if (!child1 || child1.closed) {
child1 = window.open(...);
...//rest of original code
} else {
child1.focus();
/* in case it okay to simply open the same window
for another upload; otherwise, nix this section*/
}
if (child1.opener == null) {
child1.opener = self;
}
}
CHILD: Suppose x.js has a function named 'childListener': Your process.php can provide self-learning JavaScript to the "child" page, for example:
var talkToParent = function(saywhat)
{
opener = opener || self.opener || window.opener;
if (opener != null && saywhat) {
opener.childListener(saywhat);
}
}($uploaded_filename);
This would immediately execute (from script tags, of course) if the uploaded filename were unloaded. You could alert the parent about the upload error if you set your php to deliver that instead of the filename in this case. Again this assumes the "childListener" function is in x.js or some other script called from the open page.
a source to share