在脚本加载上运行javascript函数 [英] Run a javascript function on script load

查看:83
本文介绍了在脚本加载上运行javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery。我将所有函数定义都包含在application.js中的$(document).ready事件中。我有一个函数,我想在我正在处理的页面正文中的某个地方调用。

I am using jQuery. I keep all of my function definitions wrapped in the $(document).ready event in application.js. I have a function from it that I would like to call somewhere in the body of the page I am working on.

我想知道是否有一些可用于脚本加载的.ready事件的替代品。理想情况下,我想做类似的事情:$('application.js')。ready(调用函数);​​

I was wondering if there is some alternative to the .ready event that will work on a script load. Ideally I would like to do something like: $('application.js').ready( call function );

在jQuery文档中它只提到$(文档) ).ready call但我想知道这是否可以改变或是否有一些简单的javascript替代。

In the jQuery documentation it only mentions the $(document).ready call but I was wondering if this can be altered or if there is some plain javascript alternative.

推荐答案

我想你正在寻找 $。getScript 。此jQuery函数使用 GET HTTP请求从服务器加载JavaScript文件并执行它。您可以指定一个回调函数,在脚本本身执行后执行。

I think you’re looking for $.getScript. This jQuery function loads a JavaScript file from the server using a GET HTTP request and executes it. You can specify a callback function, to be executed after the script itself has been executed.

$.getScript('foo.js', function() {
 alert('foo.js was loaded, do something cool now');
});

这篇关于在脚本加载上运行javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆