动态加载脚本 [英] Loading scripts dynamically

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

问题描述

为了响应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屋!

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