views:

1264

answers:

7

In what seems to be random occurrences, javascript files are not loading.

I believe this diagnosis is correct because a) I have code to check, b) I've stepped through the code, and c) I get “'myfunction' is undefined” error when functions in those files are used.

Sometimes this doesn’t happen for an hour, sometimes it happens every time I load the page, sometimes it happens every other time I load the page. It seems like every time I identify a consistent behavior so I can repeat it and diagnose it, it changes!

Does anybody have any idea what might be causing this?

I'm using :

  • IE version 7.0.5730.11 (had & uninstalled IE8 Beta)
  • VS2008

Right now, it appears to only be happening to my colleague and I on our development environments.

There is one script which appears to be missing more than any other. Here's the script tag.

<script language="javascript" 
        type="text/javascript" 
        src="js/Ajax.Utility.js?<%= ConfigurationManager.AppSettings["WebApp.JavaScript.FileVersion"].ToString() %>"></script>

Which evaluates to

<script language="javascript" 
        type="text/javascript" 
        src="js/Ajax.Utility.js?090324a"></script>

The version query string parameter doesn't appear to have any effect either since I've both had and not had the problem immediately after changing it.

+3  A: 

Is it not loading at all or are you calling the javascript function before the page has fully loaded? In other words, what are you using to trigger calling myfunction?

Keltex
@Keltex-The missing function is being called later in the app. And the code I said I was stepping through is variable declarations and other simple code like that.
John MacIntyre
A couple more questions: Have you tried turning off script debugging? Are you using IIS or the built-in web server?
Keltex
No, I did not turn off script debugging in IE, and I'm using the built in Visual Studio web-server. I will try that, but right now I'm having an issue where the problem is not ocurring.
John MacIntyre
+1  A: 

Another thought: are you flushing the browser cache between tests? A browser is perfectly at liberty to cache scripts as it sees fit unless prevented from doing so - normally you only noticed if editing scripts and stylesheets and images

Before I put the file version parameter in the query string, I was doing it religiously, but since then I've slacked off. I will start doing that again, but I'm pretty sure this was happening when I was clearing the cache regularly.
John MacIntyre
A: 

Instead of src="js/AjaxTry.Utility.js" try this

src="<% ResolveUrl("~/path/from/root/to/js/Ajax.Utility.js") %>
Philippe
Thanks, I know where your coming from, but it's not an issue of not finding the js file, since sometimes it's there ... actually it's ussually there.
John MacIntyre
+1  A: 

Have you checked with a tool like Fiddler that the call for that .js file is being made to either the server or the browser cache, and that the browser is actually recieving the file correctly, and not getting a 404 or some such?

You should check this on the page that's actually calling the method in question - as your path is a relative url (i.e. doesn't start with a "/"), is there any possiblity that the page you are calling it from isn't below the js folder but perhaps beside it?

Imagine a site structure like this from the root of the site:

/page1.aspx
/folder/page2.aspx
/js/Ajax.Utility.js

If both page1 and page2 have your call to the .js file, only page1 will actually be able to use retrieve it correctly - page2 will effectively be looking for:

/folder/js/Ajax.Utility.js
Zhaph - Ben Duguid
@Zhaph - Ben Duguid-since the problem is only occurring ocasionally, it can't be a path problem. But the fiddler idea is a good idea to issolate if the problem is the webserver of IE. +1
John MacIntyre
+1  A: 

Just in case the answers given have not worked -

IE7 by default does not load Javascript from local files. Usually, you'll get the information bar dropping down and indicating that. You have to click on the bar and select "Allow blocked content" and then IE7 will reload the page with Javascript enabled.

This behaviour is consistent. However, if some systems disable the display of the Information Bar, there is now way you can get to know what the problem is.

My suggestion is to add a < noscript > tag on top of your HTML page and stylize it with a bold red background using CSS so that you cannot miss it. StackOverflow uses this technique.

This way you'll surely get to know if Javascript is not loading due to Security reasons.

globetrotter
@globetrotter-Thanks, that sounds like a good technique. However, I've got a bunch of scripts, and all the others load just fine. Even this one loads ok, 60% of the time. But for some reason I cannot phathom, it occasionally doesn't load. ??? :-(
John MacIntyre
A: 

Just an FYI for anybody having the same problem.

This was never resolved, but I now believe it had something to do with Visual Studio Development Server, and I question if this would have happened had I been using IIS. Unfortunately, I no longer work on that project, so I cannot test it.

John MacIntyre
A: 

I've had this problem problem twice before. It's always a syntax error in the Javascript code. Most probably an extra bracket or a missing one. All other browsers let it through but IE7 wasn't.

Trial and error (removing chunks and chunks of code) led me to finding the solution.

I hope this helps :-)

Abdo
Thanks for the insight, but I don't think this was the problem. As I stated in the question, sometimes it gets downloaded, sometimes it doesn't. When it does load, the script is fine. But thanks again.
John MacIntyre