我可以从另一个调用一个js文件。 [英] can i call one js file from another .

查看:64
本文介绍了我可以从另一个调用一个js文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net mvc应用程序,我试图将我的所有JavaScript移动到js文件。现在我有一个名为sharedfunctions.js的地方,它在整个地方使用。

i have an asp.net mvc app and i am trying to move all of my javascript out into js files. right now i have one called sharedfunctions.js which is used all over the place.

问题是,如果我将一些代码移动到调用方法的js文件中在sharedfunctions.js文件中,它似乎不再起作用。

the issue is that if i move some code into a js file that is calling a method in the sharedfunctions.js file, it no longer seems to work.

能够在js文件中分解公共代码并允许它们实际存在的最佳方法是什么工作正常吗?

what is the best way to be able to factor out common code among js files and allow them to actually work properly ?

推荐答案

只要在页面上引用所有.js文件,所有这些文件中的函数/变量都将一旦页面加载,任何其他人都可以访问

As long as all the .js files are referenced on the page inquestion, functions/variables in all them will be accessible by any others once the page has loaded.

我突出了最后一点,因为它可能是你问题的根本原因。可能是.js文件加载的顺序是错误的。

I'm highlighting that last bit because it may well be the root cause of your problems. It may be that the order that the .js files are loading is wrong.

如果你有一些立即执行的javascript(例如它不是由页面事件触发) ,然后它可能会在您的sharedfunctions.js文件已包含在页面之前开始执行。在这种情况下,sharedfunctions.js中的函数在其他javascript执行时字面上不存在。

If you have some javascript that executes immediately (e.g. it is not triggered by a page event), then it may well start executing before your sharedfunctions.js file has been included on the page. In this case, the function in sharedfunctions.js literally won't exist at the time the other javascript executes.

看看你是否可以查看要消除的事物的顺序这些问题。例如,尝试并确保您的sharedfunctions.js作为页面上的第一个脚本文件加载。

See if you can look at the order of things to eliminate these problems. For example, try and ensure your sharedfunctions.js loads as the first script file on the page.

恕我直言,这比尝试破解现有的js文件加载更可取其他js文件的内容直接。

IMHO, this is preferable to trying to hack your existing js files to load the contents of other js files directly.

这篇关于我可以从另一个调用一个js文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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