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.

+1


a source to share


5 answers


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) { ... }

      

+1


a source


What are makewindows doing? You probably want to put this html in a template or a separate file of some type. Yes, you can call javascript from form event, onsubmit event.



0


a source


Yes, you can. You just need to call it by name, as with any function defined in the html file itself.

0


a source


If I understand your question correctly, are you trying to update some information in the popup and update this updated information on the page that opened the popup?

I think this might help you.

0


a source


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.

0


a source







All Articles