Leonardo de Jesus
Leonardo de Jesus

Reputation: 417

Script file loaded on partial view causing error

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!

Upvotes: 1

Views: 175

Answers (3)

Ibrahim Khan
Ibrahim Khan

Reputation: 20750

You can do it easily by calling profile.init() function in onload of the script tag. Hope this helps.

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

Upvotes: 0

Jack1987
Jack1987

Reputation: 727

Create a layout page and render the js files in that page, remember to use your layout in every other page you want yours scripts loaded.

Upvotes: 0

j.v.
j.v.

Reputation: 997

You can reference profile.js on the layout or main view, that way you'll know that profile is going to be defined when you call it in partial view.

Upvotes: 1

Related Questions