从表单结果调用javascript函数

时间:2022-04-09 15:59:09

So I have a html page(actually generated with php), that will make a popup window containing a form. All my javascript stuff is in a seperate javascript file. I want to know if it is possible to call a function from my javascript file from the form resulttext.

所以我有一个html页面(实际上用php生成),它将创建一个包含表单的弹出窗口。我所有的javascript东西都在一个单独的javascript文件中。我想知道是否可以从表单resulttext中调用我的javascript文件中的函数。

Here is the first html page

这是第一个html页面

<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 uploading a file, 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 can I call this from a normal html page?

上传文件后,将生成显示文件名称的结果文本。在x.js中,我有一个函数来更新原始窗口中的div。我可以从普通的html页面调用它吗?

Here is the code for maewindows

这是maewindows的代码

function makewindows(html){
child1 = window.open ("about:blank");
child1.document.write(html);
child1.document.close(); 
}

I don't want to call the function with onclick, i want to call it from the result text of the form.

我不想用onclick调用该函数,我想从表单的结果文本中调用它。

5 个解决方案

#1


You want to access a Javascript function declared in the same page that opened a window using window.open from script running within the opened window?

您想要在打开的窗口中运行的脚本中使用window.open来访问在打开窗口的同一页面中声明的Javascript函数吗?

window.opener is what you're looking for.

window.opener是你正在寻找的。

For example, if you have a have a function defined in x.js as function foobar(s) (and x.js is included in the main window), you could return something like the following as the response from process.php:

例如,如果你有一个在x.js中定义的函数作为函数foobar(和x.js包含在主窗口中),你可以返回类似下面的内容作为process.php的响应:

<script type="text/javascript">
    window.opener.foobar('returned text');
    window.close();
</script>

And the string "returned text" is passed back to the foobar function.

并且字符串“返回的文本”被传递回foobar函数。

The reason this works is that anything defined with the function keyword from global scope is automatically added as a property of the current window object. If your function isn't defined in the global scope, you can force it to be a property of window like this:

这样做的原因是,使用全局作用域中的function关键字定义的任何内容都会自动添加为当前窗口对象的属性。如果您的函数未在全局范围中定义,则可以强制它为窗口的属性,如下所示:

window.foobar = function(s) { ... }

#2


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

makewindows做什么?您可能希望将该html放在模板或某种类型的单独文件中。是的,你可以从表单事件,onsubmit事件中调用javascript。

#3


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

是的你可以。您只需按名称调用它,就像在html文件中定义的任何函数一样。

#4


If I'm understanding your question correctly, you're trying to update some information in the popup window, and have that updated information reflected in the page that opened the popup window?

如果我正确理解您的问题,您正在尝试更新弹出窗口中的某些信息,并在打开弹出窗口的页面中反映更新的信息?

I think this might help you.

我想这可能会对你有所帮助。

#5


Tobias is right.

托比亚斯是对的。

But with no opener property, the main window and target window cannot communicate-- there's no tie.

但是没有开启属性,主窗口和目标窗口无法通信 - 没有关系。

This dated article illustrates one solution for onclicks, albeit one with targets. Might be fun for you to play with. http://homepage.mac.com/bigeuniverse/tests/targetwindows/

这篇过时的文章说明了onclicks的一个解决方案,尽管有一个目标。玩你可能很有趣。 http://homepage.mac.com/bigeuniverse/tests/targetwindows/

It is best, today, however, to do this with listeners and functions tied to them. I would clearly define the opener in your 'makewindows' function, as well as define the window.

然而,今天最好用听众和与之相关的功能来做到这一点。我会在'makewindows'函数中清楚地定义开启器,以及定义窗口。

PARENT: 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's okay to simply open the same window 
          for another upload; otherwise, nix this section*/
    }
    if (child1.opener == null) {
          child1.opener = self;
    }        
 }

CHILD: Supposing that x.js has a function named 'childListener': Your process.php could deliver a self-executing JavaScript function to the 'child' page, like:

CHILD:假设x.js有一个名为'childListener'的函数:你的process.php可以为'child'页面提供一个自动执行的JavaScript函数,如:

var talkToParent = function(saywhat) 
{
    opener = opener || self.opener || window.opener;
    if (opener != null  && saywhat) {
        opener.childListener(saywhat);
    }
}($uploaded_filename);

This would immediately execute (from within script tags of course), as long as the uploaded filename was delivered. You could actually alert the parent to an error in upload if you set your php to deliver that in lieu of the filename, in that event. Again, this assumes the 'childListener' function is in x.js or some other script called from the opener page.

只要上传的文件名已经发送,这将立即执行(当然,从脚本标记内)。如果您将php设置为代替文件名,那么您实际上可以提醒父母上传错误。同样,这假设'childListener'函数位于x.js或从opener页面调用的其他脚本中。

#1


You want to access a Javascript function declared in the same page that opened a window using window.open from script running within the opened window?

您想要在打开的窗口中运行的脚本中使用window.open来访问在打开窗口的同一页面中声明的Javascript函数吗?

window.opener is what you're looking for.

window.opener是你正在寻找的。

For example, if you have a have a function defined in x.js as function foobar(s) (and x.js is included in the main window), you could return something like the following as the response from process.php:

例如,如果你有一个在x.js中定义的函数作为函数foobar(和x.js包含在主窗口中),你可以返回类似下面的内容作为process.php的响应:

<script type="text/javascript">
    window.opener.foobar('returned text');
    window.close();
</script>

And the string "returned text" is passed back to the foobar function.

并且字符串“返回的文本”被传递回foobar函数。

The reason this works is that anything defined with the function keyword from global scope is automatically added as a property of the current window object. If your function isn't defined in the global scope, you can force it to be a property of window like this:

这样做的原因是,使用全局作用域中的function关键字定义的任何内容都会自动添加为当前窗口对象的属性。如果您的函数未在全局范围中定义,则可以强制它为窗口的属性,如下所示:

window.foobar = function(s) { ... }

#2


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

makewindows做什么?您可能希望将该html放在模板或某种类型的单独文件中。是的,你可以从表单事件,onsubmit事件中调用javascript。

#3


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

是的你可以。您只需按名称调用它,就像在html文件中定义的任何函数一样。

#4


If I'm understanding your question correctly, you're trying to update some information in the popup window, and have that updated information reflected in the page that opened the popup window?

如果我正确理解您的问题,您正在尝试更新弹出窗口中的某些信息,并在打开弹出窗口的页面中反映更新的信息?

I think this might help you.

我想这可能会对你有所帮助。

#5


Tobias is right.

托比亚斯是对的。

But with no opener property, the main window and target window cannot communicate-- there's no tie.

但是没有开启属性,主窗口和目标窗口无法通信 - 没有关系。

This dated article illustrates one solution for onclicks, albeit one with targets. Might be fun for you to play with. http://homepage.mac.com/bigeuniverse/tests/targetwindows/

这篇过时的文章说明了onclicks的一个解决方案,尽管有一个目标。玩你可能很有趣。 http://homepage.mac.com/bigeuniverse/tests/targetwindows/

It is best, today, however, to do this with listeners and functions tied to them. I would clearly define the opener in your 'makewindows' function, as well as define the window.

然而,今天最好用听众和与之相关的功能来做到这一点。我会在'makewindows'函数中清楚地定义开启器,以及定义窗口。

PARENT: 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's okay to simply open the same window 
          for another upload; otherwise, nix this section*/
    }
    if (child1.opener == null) {
          child1.opener = self;
    }        
 }

CHILD: Supposing that x.js has a function named 'childListener': Your process.php could deliver a self-executing JavaScript function to the 'child' page, like:

CHILD:假设x.js有一个名为'childListener'的函数:你的process.php可以为'child'页面提供一个自动执行的JavaScript函数,如:

var talkToParent = function(saywhat) 
{
    opener = opener || self.opener || window.opener;
    if (opener != null  && saywhat) {
        opener.childListener(saywhat);
    }
}($uploaded_filename);

This would immediately execute (from within script tags of course), as long as the uploaded filename was delivered. You could actually alert the parent to an error in upload if you set your php to deliver that in lieu of the filename, in that event. Again, this assumes the 'childListener' function is in x.js or some other script called from the opener page.

只要上传的文件名已经发送,这将立即执行(当然,从脚本标记内)。如果您将php设置为代替文件名,那么您实际上可以提醒父母上传错误。同样,这假设'childListener'函数位于x.js或从opener页面调用的其他脚本中。