开发者

Include a JavaScript file [duplicate]

This question already h开发者_如何学Pythonas answers here: Closed 12 years ago.

Possible Duplicates:

Include javascript file inside javascript file?

How do you dynamically load a javascript file? (Think C's #include)

Is there a way to include a JavaScript file from a .js file? You know: like how I link .css files at the top of a .css file.

xxx.js contains:

Some sort of @import yyy.js
followed by other js commands.


No, there isn't. You either have to include it in the HTML or use JavaScript to create a <script> tag and attach it to the document.


this is my workaround to do this:

if (typeof (jQuery) == "undefined") 
    document.write(unescape("%3Cscript src='" + server_url + "/js/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E"));

of course, for anything other than jquery you'd have to figure out if you needed it or not then do the same with that file.

server_url needs to be set also.

here is a working example of this

http://my.digitalscout.com/Widgets/schedule.html


You can't import, but you can reference.

/// <reference path="yyy.js" />

Reference just adds intellisense support.


This can be done if you are in a server side environment. For instance, in php you could use the include statement, though you would have to label the file '.php' instead of '.js' so the preprocesser will run on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜