0

Is there a method of flushing or registering inline variables prior to external scripts are loaded?

i.e. Have a script block at the top of the MasterPage.Master Body

<script type="text/javascript">
  var promoteSuggestionID = "<%= SuggestionID %>";
</script>

Bundled Script at the bottom of the Page Body.

  <%: Scripts.Render("~/scripts/Timeline") %>

Timeline script

(function (TimeLine) {
   TimeLine.PromoteSuggestion = promoteSuggestionID;
 //....Main body of script

}((window.TimeLine) ? window.TimeLine : (window.TimeLine = {})));

using Chrome the following error is shown when the line in the timeline script is hit.

Uncaught ReferenceError: promoteSuggestionID is not defined(anonymous function)

Previously we were loading the timeline script via modernizer. When I use chrome i can see that the default.aspx code has the promotedSuggestionID set if i put a breakpoint on the Timeline.PromotedSuggestion code. Its as if this scriptblock hasn't been registered yet, is there a way i can set the priority?

2
  • 2
    If promoteSuggestionID is defined at the very top of the body and Timeline is loaded at the bottom this should work. Isn't there any other error? Are you sure Timeline is loaded after promoteSuggestionID definition? Commented Mar 2, 2015 at 12:18
  • It certainly looks that way. I've decided to wrap initialising code (which uses window variables set by codeblocks) in a document.ready Commented Mar 2, 2015 at 13:38

1 Answer 1

1

You should really never have JavaScript that is just on the page to run like this for that very reason. What I mean is, if I have JavaScript that will execute upon being parsed then I have no guarantee when it will execute. And, depending in the browser, the page order, etc it may very well run before everything is fully loaded.

Change your function to just be a definition, and wire it up to be called on document.ready. That is the proper way, and you can guarantee that the DOM and all referenced JS is good to go at that point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.