2

Situation:

I am building a page in ASP.NET from an HTML file with markup and javascript, several .js files, stylesheets, and some additional inline javascript on the ASP.NET page. Working in Visual Studio 2010 with ASP>NET development server host.

The XML and js files are supplied by a third party and cannot be altered, one of the js files is 'our' file and can be altered.

The HTML file is a questionnaire that can be filled out. Currently we have a desktop application that 'hosts' the HTML file and saves the answers.

The overall goal is to host the questionnaire in a website so that we can duplicate functionality from the desktop application on the web. Javascript will be used to duplicate what the desktop application does not ASP.NET.

The HTML file contains <script> links to the js files and a large block of inline javascript after the markup. A line (not a function, an actual line of code) in the inline javascript calls a function in 'our' include file.

In the ASP.NET code I am extracting content from the HTML file and applying it to build the ASP.NET page. CSS links, scriptlinks, the markup, inline scriptblocks etc. So the output response looks like this:

<html>
    <head>
        <link to CSS>
        <script src=thirdparty.js>
        <script src=our.js>
    </head>
    <body>
        third party HTML markup
        <script>
            line of code here that calls function in our.js;
        </script>
        <script>
            desktop duplication code here;
        </script>
    </body>
</html>

If I copy the content of the js files and build them into the page as inline script blocks everything works fine. If I include them as links then sometimes the line mentioned above throws a 'function undefined' error in the debugger. i.e. It acts as if the javascript file is not loaded/included. As far as I know this should not happen as all the javascript should be loaded and parsed before any of it is run.

Question:

Is the javascript loaded out of order/asynchronously? If so how can I force all the javascript to load and parse before the line is run.

Notes:

Yes I do have to build the page in ASP.NET, the 'files' are dynamic and actually read from a web service so can't be prepared beforehand. What I'm trying to create is a generic approach to handle several suppliers questionnaires.

The third party javascript and markup only works in IE as it's a legacy system.

I can't show actual code for obvious reasons.

James Donnelly
  • 117,312
  • 30
  • 193
  • 198
UserEleventyOne
  • 252
  • 1
  • 11

2 Answers2

3

I found the answer which had nothing to do with the problem I stated above.

The DOCTYPE tag was absent when the page worked but present when the page failed. So the problem is that the third party markup doesn't meet any form of standards and in order to get it working the browser must be in quirks mode, whihc is accomplished by leaving out the DOCTYPE tag.

Not obvious at all.

UserEleventyOne
  • 252
  • 1
  • 11
1

Have you tried using the document on ready event to fire the "line of code here that calls function in our.js;"

If you were using jQuery you would do something like this:

<script type="text/javascript">
  $(document).ready(function() {
    line of code here that calls function in our.js;
  })
</script>

Take a look at $(document).ready equivalent without jQuery if you don't want to use jQuery

Another alternative would be to add the tag at the very bottom of your ASP.Net page which calls your function e.g.:

<html>
 <head>
  <link to CSS>
  <script src=thirdparty.js></script>
  <script src=our.js></script>
 </head>
 <body>
  third party HTML markup
  <script>
  desktop duplication code here;
  </script>
  <script>
   line of code here that calls function in our.js;
  </script>
 </body>
</html>
Community
  • 1
  • 1
kzhen
  • 2,840
  • 15
  • 20
  • I'm not allowed to change the third party code and document.ready is already in use by the third party. As is the onload event... I'm really hampered by the pre-existing conditions. – UserEleventyOne May 04 '12 at 09:44
  • are you allowed to include additional 3rd party libraries such as jQuery? – kzhen May 09 '12 at 10:16