如何从其他页面调用 Javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5868917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a Javascript function from other page?
提问by zac1987
I have 5 javascript functions index.php :
我有 5 个 javascript 函数 index.php :
1) showMsg(); //this display 50 latest messages
1) showMsg(); //this display 50 latest messages
2) showPopUpBox(); // this I use jquery to load the textbox and send button from typingMsg.php
2) showPopUpBox(); // this I use jquery to load the textbox and send button from typingMsg.php
3) hidePopUpBox(); // this hide pop up box
3) hidePopUpBox(); // this hide pop up box
4) checkNewMsg(); // this will be auto reloaded every 5 sec to check if there is new message, it will show the numbers counts of total new messages.
4) checkNewMsg(); // this will be auto reloaded every 5 sec to check if there is new message, it will show the numbers counts of total new messages.
5) ShowNewMsg(); // this function will be called when the user click the "show new messages" button.
5) ShowNewMsg(); // this function will be called when the user click the "show new messages" button.
After a user typing message on the text box on the pop up box then after he click "Send" button, ajax will call messagePost.php to submit the message to database, as the following code :
当用户在弹出框的文本框中输入消息后,点击“发送”按钮后,ajax 将调用 messagePost.php 将消息提交到数据库,如下代码:
$(function() {
$(".button").click(function() {
$.ajax({
type: "POST",
url: "messagePost.php",
data: dataString,
success: function() {
$('textarea.expand25-75').val('');
showMsg(); //this is my problem
hidePopUpBox(); //this is my problem too
}
});
return false;
});
});
As u can see from the codes above, the function of showMsg(); hidePopUpBox(); cannot be called because the functions are not on this page, my question is How to call the javascript function from different page?
从上面的代码可以看出,showMsg(); 隐藏弹出框();无法调用,因为函数不在此页面上,我的问题是如何从不同页面调用 javascript 函数?
采纳答案by abahgat
You'll have to include all the functions you need in each of the pages that will use them (index.phpand messagePost.php, I guess).
您必须在将使用它们的每个页面中包含您需要的所有功能(我猜是index.php和messagePost.php)。
That is normally done by grouping all the correlated functions in .js
files and then including them by using the <script>
tag.
这通常是通过将.js
文件中的所有相关函数分组,然后使用<script>
标签将它们包含在内来完成的。
By the way, some of the other answers include, correctly, top
and opener
. All of the suggested options make your code work, but in general I suggest you to import the functions right in the pages that need them.
顺便说一句,其他一些答案包括正确的top
和opener
。所有建议的选项都可以使您的代码正常工作,但总的来说,我建议您直接在需要它们的页面中导入函数。
For example, it may make sense to import the hidePopUpBox()
function right within the popup (which will be using than), rather than in the parent window.
例如,hidePopUpBox()
在弹出窗口中(将使用 than)而不是在父窗口中导入函数可能是有意义的。
回答by BrunoLM
Try to use
尝试使用
top.showMsg();
top.hidePopUpBox();
top
refers to the top most window
object of your page. If you are in an iframe for example.
top
指的是window
页面最顶部的对象。例如,如果您在 iframe 中。
Or try opener
if you are in a popup (called by window.open
)
或者尝试opener
是否在弹出窗口中(由 调用window.open
)
opener.showMsg();
opener.hidePopUpBox();
回答by David M?rtensson
If, as I understand, the above script is run in a popup you can reach the scripts int the base page using opener object.
据我了解,如果上述脚本在弹出窗口中运行,您可以使用 opener 对象访问基本页面中的脚本。
$(function() {
$(".button").click(function() {
$.ajax({
type: "POST",
url: "messagePost.php",
data: dataString,
success: function() {
$('textarea.expand25-75').val('');
opener.showMsg(); //this is my problem
opener.hidePopUpBox(); //this is my problem too
}
});
return false;
});
});
opener will always exist in a window that was opened from another window pointing back.
opener 将始终存在于从另一个指向回的窗口打开的窗口中。
回答by Marwan
no no i can see where is the problem i think the problem is with
不 不 我能看出问题出在哪里 我认为问题出在
$(function() {
// You Are Here Not in contact with outside script
});
i think this is called javascript closure or some thing i think you should use instead of that the
我认为这称为 javascript 闭包或某些我认为您应该使用的东西而不是
window.onload=function()
{
$(".button").click(function() {
$.ajax({
type: "POST",
url: "messagePost.php",
data: dataString,
success: function() {
$('textarea.expand25-75').val('');
opener.showMsg(); //this is my problem
opener.hidePopUpBox(); //this is my problem too
}
});
return false;
});
}
to be in contact with other functions in same page that if i got you right
与同一页面中的其他功能联系,如果我没听错的话
Regards
问候
回答by Nicola Peluchetti
What do you mean with "on another page"?If they all are in index.php you should have no problem calling them. Since i see you are using jQuery, i suggest you put your javascript function outside of $(Document).ready(function(){ });
“在另一个页面上”是什么意思?如果它们都在 index.php 中,那么调用它们应该没有问题。因为我看到你在使用 jQuery,我建议你把你的 javascript 函数放在 $(Document).ready(function(){ }); 之外。
because they have to stay outside, but i don't think it's what you are saying.
因为他们必须待在外面,但我认为这不是您所说的。