2011-05-03 107 views
3

我有5周JavaScript函数的index.php:如何从其他页面调用Javascript函数?

1)showMsg(); //this display 50 latest messages

2)showPopUpBox(); // this I use jquery to load the textbox and send button from typingMsg.php

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.

5)ShowNewMsg(); // this function will be called when the user click the "show new messages" button.

在用户键入m之后对再后,他点击“发送”的弹出框的文本框中essage按钮,阿贾克斯将调用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; 
}); 
}); 

为u可以从看代码如上,showMsg()的功能; hidePopUpBox();不能被称为,因为功能不在这个页面上,我的问题是如何从不同的页面调用JavaScript功能?

+1

我能问你为什么不把你的可重用的JavaScript功能.js文件,并采取refrence在每一页ü需要使用它的功能? 还是做这个功能Server Business与index.php页面有关? – Marwan 2011-05-03 11:45:33

+1

'如何从不同的页面调用javascript函数?'和什么是不同的页面? – epascarello 2011-05-03 11:46:31

+0

该功能在该范围内是否可见?阿贾克斯是否成功?另一件事是,其他页面是否可以调用,或者你是否正确地将它添加到当前页面?像'' – ace 2011-05-03 11:50:30

回答

2

你必须包括你在每个将要使用它们的网页需要的所有功能(的index.phpmessagePost.php,我猜)。

这通常是通过将.js文件中的所有相关函数进行分组,然后使用<script>标记包含它们来完成的。


顺便说一句,一些其他的答案包括正确,topopener。所有建议的选项都会使您的代码正常工作,但通常我建议您在需要它们的页面中正确导入这些功能。

例如,在弹出窗口(将使用than)内导入hidePopUpBox()函数是合理的,而不是在父窗口中。

+0

我确实说过L :) – Marwan 2011-05-03 12:11:10

0

你对“在另一个页面上”有什么意思?如果他们都在index.php中,你应该没有问题打电话给他们。 因为我看到你使用的是jQuery,我建议你把你的javascript函数放在$(Document).ready(function(){ })之外。

因为他们不得不待在外面,但我不认为这就是你所说的。

1

如果据我所知,上面的脚本是在弹出窗口中运行的,您可以使用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; 
}); 
}); 

开启器将始终存在于从另一个窗口向后指向的窗口中。

0

,不,不,我可以看到哪里出了问题,我认为这个问题是

$(function() { 
// You Are Here Not in contact with outside script 
}); 

我想这就是所谓的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; 
}); 
} 

是在 同一页面与其他功能的接触,如果我得到你的权利

问候

2

尝试使用

top.showMsg(); 
top.hidePopUpBox(); 

top是指页面的最顶端window对象。例如,如果您在iframe中。

或者尝试opener,如果你是在一个弹出式(由window.open调用)

opener.showMsg(); 
opener.hidePopUpBox(); 
相关问题