javascript 从外部调用准备好的 jQuery 内部定义的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3402877/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:04:41  来源:igfitidea点击:

Calling a function defined inside jQuery ready from outside of it

javascriptjquery

提问by IsmailS

My aspx page:-

我的aspx页面:-

  <script src="js/jquery-1.4.2.js" type="text/javascript"></script>
  <script src="js/jquery-ui-1.8.2.custom.js" type="text/javascript"></script>

  <script type="text/javascript">
    $(document).ready(function () {
      //lots of other code here....
      function showMessage(){
        $("#msgDiv").dialog({
                        modal: true,
                        buttons: {
                                  Ok: function() {
                                          $(this).dialog('close');
                                      }
                        },
                        resizable: true,
                        show: "explode",
                        position: "center",
                        closeOnEscape: true,
                        draggable: false
                      });
      }
    });
   </script>

Another aspx pop up page which is triggered from the above page

从上面的页面触发的另一个aspx弹出页面

<script type="text/javascript">

    window.opener.document.getElementById("msgDiv").innerHTML = <%=MessageToShow%>; //works very well for me.
    window.opener.document.showMessage(); // I am unable to access it like this?
    window.close();

</script>

Basically I want to call showMessage()from the pop up window. I also have other logics to perform in both pages.

基本上我想showMessage()从弹出窗口调用。我还有其他逻辑要在两个页面中执行。

回答by Bennor McCarthy

Declare your function like this inside the document ready:

在准备好的文档中像这样声明你的函数:

$(document).ready(function() {

    window.showMessage = function() {
        //...
    };

});

Then you should be able to call it from the other document like this:

然后你应该能够像这样从另一个文档中调用它:

window.opener.showMessage();

And because it's in the global scope, you can call it from within the main document just by calling

并且因为它在全局范围内,您可以通过调用从主文档中调用它

showMessage();

回答by James Curran

As far as I know, what you want to do, can't be done. So, what exactly can we do?

据我所知,你想做的,是做不到的。那么,我们究竟能做什么呢?

I think the simpliest think would be to move showMessageoutside of the readyfunction, and just call it from within.

我认为最简单的想法是移到函数showMessage之外ready,然后从内部调用它。

Now, if it really must be defined within that function, make it a named function:

现在,如果确实必须在该函数中定义它,请将其设为命名函数:

  function calledonready()
  {
      /// stuff
      function showMessage(){ 
      /// stuff
      }
   }

$(document).ready(calledonready);

window.opener.document.calledonready.showMessage(); 

回答by jAndy

You can just declare showMessage()public by putting it directly under your <script>tag. Well that turns out as bad practice actually, but it would work.

您可以showMessage()通过将其直接放在您的<script>标签下来声明public 。好吧,事实证明这是不好的做法,但它会起作用。

The better solution should always be to use your own namespace. Declare an objectwhere all of your application logic can take place.

更好的解决方案应该始终是使用您自己的命名空间。声明object所有应用程序逻辑可以发生的位置。

<script>
var MyApp = {
    showMessage:   function(){
       // do something
    }
};

Later in your code you can access this object from everywhere with

稍后在您的代码中,您可以从任何地方访问此对象

MyApp.showMessage();

You can bring that pattern to an extend by using closures. A lot of smart people developed nice patterns for the ecmascripttherefore. A good read like always is `Javascript: the good parts" by Douglas Crockford.

您可以通过使用扩展该模式closures。许多聪明人为此开发了很好的模式ecmascript。Douglas Crockford 所著的“Javascript:好的部分”总是值得一读。

var MyApp = function(){
    var my_private_data = "version 1.0.0",
        foo             = "bar";

     return {
        getfoo          = function(){
             return(foo);
        },
        showMessage     = function(){
             // do something
        }
     };
};

var app = MyApp();

app.showMessage();

This is all about private dataand namespacing. That way no other script on your side can possibly change any date you hold within your closuredobject. The idea can even taken much further by creating inheritanceand shared data(semi protected), but I guess this is another story.

This is all about private dataand namespacing. That way no other script on your side can possibly change any date you hold within your closuredobject. The idea can even taken much further by creating inheritanceand shared data(semi protected), but I guess this is another story.