动态加载脚本 [英] Loading scripts dynamically
问题描述
为了响应Ajax请求,我在代码中动态加载了几个YUI脚本。 DOM和页面在请求完成时被完全加载 - 这是对用户事件的响应。
我把标签添加到头部,就像孩子一样。但是我偶然发现了一些问题:
我添加了两个YUI脚本, CDN和我自己负责创建对象,添加事件侦听器和呈现YUI小部件的内联脚本。但是,当我的脚本运行YUI脚本没有加载,但给我错误,并没有按我的预期运行。
有一种方法可以只运行我的脚本(或定义一个函数来运行)当YUI脚本完全加载?
您可以使用 setTimeout
运行一些函数来检查它是否被加载 - 检查类似于
$ b
if(typeof YUI_NAMESPACED_THING!== undefined)runCode()
编辑谢谢CMS
I'm loading a few YUI scripts dynamically in my code in response to an Ajax request. The DOM and the page is fully loaded when the request is made - it's a response for an user event.
I add the tag to head, as children. But I stumbled in a few problems:
I add two YUI scripts hosted at the Yahoo! CDN and an inlined script of my own responsible for creating object, adding event listeners and rendering the YUI widgets. But I when my script run the YUI scripts are not loaded yet giving me errors and not running as I expect.
There's a way to only run my script (or define a function to be run) when YUI scripts are fully loaded?
You could use a setTimeout()
to run some function that just checks if it's loaded - check something like
if (typeof YUI_NAMESPACED_THING !== "undefined") runCode()
EDIT Thanks, CMS
这篇关于动态加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!