Script file loaded on partial view causing error

时间:2015-12-04 21:18:08

标签: javascript c# asp.net-mvc

I have a mvc.net application and on some pages I load a partial view via Ajax, returning the partial view already rendered to my script.

Everything looks fine but I am facing an issue now. On one of my partial views, I have a java script file being loaded on the conventional way. And right after it I am using the class defined by that script

<script src="profile.js" />
<script >
  profile.init();
<script />

When this pages is returned rendered by my Ajax calls, depending on the time the profile.ja takes to be loaded, the init funcition is not being called saying that the profile is undefined.

If I run this local, the script is loaded fast enough and everything executes. But running this on a test environment where the script is loaded from cloudfront it takes more time and the function is executed before.

Is there a way that I can only execute the script after I know for sure it has been downloaded. Document.ready does not work cause the partial view is being loaded by Ajax

It is important to say that by some crazy stuff in our code I cannot just simply put this on my parent page or on _layout Any help would be appreciated

Thanks!

3 个答案:

答案 0 :(得分:1)

您可以在布局或主视图上引用profile.js,这样您就知道在部分视图中调用它时将定义该配置文件。

答案 1 :(得分:0)

创建一个布局页面并在该页面中渲染js文件,记得在你希望你的脚本加载的每个其他页面中使用你的布局。

答案 2 :(得分:0)

您可以通过调用profile.init()标记的onload中的script功能轻松完成此操作。希望这会有所帮助。

<script onload="profile.init()" src="profile.js"></script>