So imagine a piece of Javascript as the first script on a page along the lines of
var MySuperObject = new (function () {
this.SuperObjectInit();
})();
Now imagine that everything that proceeds this script (or a large portion therein) requires the SuperObject to have met its load conditions and loaded correctly.
Assuming for whatever reason the loading of the object fails I need to abort loading the rest of the page and the scripts in particular.
I know the majority of you are going to scream why not have your function issue a callback onSuccess and onFailed but the problem is this is in a ASP.Net project with masterpages, nestedmasterpages, usercontrols and so forth (each of which have their own dependencies and scripts); rendering such an approach problematic.
The other option (I assume) is to use window.location = "myErrorPage.html"; but I dont like the idea of ha开发者_运维知识库ving to create another page for an error message or the fact that it causes a redirect.
What I am hoping to do is something along the lines of
StopLoadingPage();
document.write("Error has occurred");
But not sure how this might be accomplished.
What you're trying to achieve isn't how web pages are designed, so as far as I know it is not possible. Logically, you can only stop in Javascript what was started in Javascript, so the only workaround I can think of is to load the dependent scripts asynchronously.
I know this isn't the solution you're looking for, but if there's no other alternative (and I don't think there is), have a look at RequireJS, which handles your JS dependencies for you (no need for callbacks etc...): http://requirejs.org/
精彩评论