从表单结果中调用一个javascript函数 [英] calling a javascript function from a form result
问题描述
这是第一个html页面
< script type =text / javascriptsrc =x.js>< / script>< h1> hello< / h1>
< div id ='form'>
< a href ='#'onclick =makewindows('< form action =process.phpmethod =postenctype =multipart / form-data>< label for =文件>文件名:< / label>< input type =filename =fileid =file/>< br />< input type =submitname =submit value =Submitonclick =/>< / form>'); return false;>
点击此处< / a>
< / div>
上传文件后,会生成结果文本,显示文件的名称。在x.js中我有一个函数来更新原始窗口中的div。我可以从一个普通的html页面调用这个吗?
这是maewindows的代码
函数makewindows(html){
child1 = window.open(about:blank);
child1.document.write(html);
child1.document.close();
}
我不想用onclick调用函数,我想调用它来自于表单的结果文本。
你想访问一个在同一个页面中声明的Javascript函数,使用window.open从脚本运行在打开的窗口内?
中, code> x.js 作为函数foobar(s)
(和 x.js
包含在主窗口中),您可以返回类似于以下内容的响应: process.php
:
< script type =text / javascript>
window.opener.foobar('returned text');
window.close();
< / script>
字符串returned text被传回给 foobar code> function。
这个原理的作用是用全局作用域中的函数
关键字定义的任何东西会自动添加为当前窗口
对象的属性。如果你的函数没有在全局范围中定义,你可以强制它成为 window
这样的属性:
window.foobar =函数(s){...}
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.
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 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?
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 with onclick, i want to call it from the result text of the form.
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.opener is what you're looking for.
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
:
<script type="text/javascript">
window.opener.foobar('returned text');
window.close();
</script>
And the string "returned text" is passed back to the foobar
function.
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:
window.foobar = function(s) { ... }
这篇关于从表单结果中调用一个javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!