Javascript 从JS运行jquery函数
对不起,我的问题很难说,但今天对我来说什么都不管用 我正在创建一个Javascript 从JS运行jquery函数,javascript,jquery,cordova,phonegap-build,Javascript,Jquery,Cordova,Phonegap Build,对不起,我的问题很难说,但今天对我来说什么都不管用 我正在创建一个Phonegap应用程序,并将PushWoosh API集成到我的应用程序中。收到推送通知后,我想再次运行以前的函数,以便更新数据 Pushwoosh的JS函数如下: document.addEventListener('push-notification', function(event) { var title = event.notification.title; var userDa
Phonegap
应用程序,并将PushWoosh API集成到我的应用程序中。收到推送通知后,我想再次运行以前的函数,以便更新数据
Pushwoosh的JS函数如下:
document.addEventListener('push-notification',
function(event) {
var title = event.notification.title;
var userData = event.notification.userdata;
var notification = event.notification;
if (typeof(userData) != "undefined") {
console.warn('user data: ' + JSON.stringify(userData));
}
var object = JSON.parse(notification.u);
window.runPushFunctions(object.active, object.open); //Runs a jQuery function I have created..
}
);
现在window.runPushFunctions
如下所示:
$(document).ready(function() {
window.runPushFunctions = function(active, open) {
if (active != null || active != undefined) {
$('.hubs-page').removeClass('hubs-active').hide().eq(active).show().addClass('hubs-active');
}
if (open == 2) {
$('html').addClass('hubs-opening');
}
//Trying to run functions from jQuery file that will get data from database and so on..
received();
sent();
checkFriends();
};
});
(function($) {
'use strict';
function checkFriends () {
$.getJSON('url',function(data){
$.each(data,function(index,value){
//Do something with value
});
});
}
但是由于某种原因,我不能不运行received()
,sent()
,checkFriends()
这些函数在它们自己的文件中设置如下:
$(document).ready(function() {
window.runPushFunctions = function(active, open) {
if (active != null || active != undefined) {
$('.hubs-page').removeClass('hubs-active').hide().eq(active).show().addClass('hubs-active');
}
if (open == 2) {
$('html').addClass('hubs-opening');
}
//Trying to run functions from jQuery file that will get data from database and so on..
received();
sent();
checkFriends();
};
});
(function($) {
'use strict';
function checkFriends () {
$.getJSON('url',function(data){
$.each(data,function(index,value){
//Do something with value
});
});
}
Im按以下顺序包括文件:
file.js -> received(); sent();
file.js -> checkFriends();
file.js -> pushnotifications
非常感谢您提供的任何帮助我不确定是否理解您的问题,但在我看来,您正在函数范围内定义函数checkFriends。如果需要访问该函数定义,则需要在可从全局范围引用的对象上声明它。显然,最简单的方法是将其连接到窗口,尽管有很多理由不这样做
window.checkFriends = function(){//code that does stuff};
我不确定我是否理解您的问题,但在我看来,您似乎在函数范围内定义函数checkFriends。如果需要访问该函数定义,则需要在可从全局范围引用的对象上声明它。显然,最简单的方法是将其连接到窗口,尽管有很多理由不这样做
window.checkFriends = function(){//code that does stuff};
正如这里的另一个答案所说,您正在确定方法定义的范围,以便在包含方法之外的任何地方都无法访问它们
(function($) {
这是一个方法定义。任何非全局声明的变量或函数都不能在外部访问。因此,您需要在其他地方定义这些函数,或者使它们成为全局函数,以便可以访问
如果要在其他地方定义它们,只需将函数定义移到同一文件的顶部,即(function($){})(
范围之外
如果改为使用全局定义,则需要稍微更改方法的定义行:而不是
function foo() { }
你需要
window.foo = function() { }
这将为窗口
范围中的对象分配一个匿名声明的函数,该对象是全局可访问的。然后,您可以使用
window.foo();
或者干脆
foo();
因为它在
窗口中
范围内。正如这里的另一个答案所说,您正在确定方法定义的范围,以便在包含方法之外的任何地方都无法访问它们
(function($) {
这是一个方法定义。任何非全局声明的变量或函数都不能在外部访问。因此,您需要在其他地方定义这些函数,或者使它们成为全局函数,以便可以访问
如果要在其他地方定义它们,只需将函数定义移到同一文件的顶部,即(function($){})(
范围之外
如果改为使用全局定义,则需要稍微更改方法的定义行:而不是
function foo() { }
你需要
window.foo = function() { }
这将为窗口
范围中的对象分配一个匿名声明的函数,该对象是全局可访问的。然后,您可以使用
window.foo();
或者干脆
foo();
因为它在
窗口中
范围内。这些函数在哪里定义?收到();发送();checkFriends();我已经用fileorder更新了我的答案,这些函数是如何定义的?收到();发送();checkFriends();我已经用fileorder更新了我的答案,谢谢,这是我在另一个函数中必须做的。但是它现在可以工作了:)谢谢,这是我在另一个函数中必须做的。但是它现在可以工作了:)谢谢,这是我在另一个函数中必须做的。但是它现在可以工作了:)谢谢,这是我在另一个函数中必须做的。但它现在起作用了:)