0

I am using some JavaScript in my ASP.Net page like this:-

<script type="text/javascript" src="MyScript.js"></script>
<script type="text/javascript">
    function validate_everything()
    {
        do_foo();
    }
</script>

where MyScript.js exists in the same directory as the Default.aspx, and contains this (only):-

function do_foo()
{
    var fred = 2;
    return fred;
}

When I attempt to run this (using Internet Explorer) it produces the error:-

0x800a1391 - JavaScript runtime error: 'do_foo' is undefined

The real code (with actual useful work) handled in the same way was working fine yesterday, and fine earlier this morning. I modified it and this error started appearing; I reverted the changes and the error still appears. Before, using Chrome, the real version worked. Now, nothing happens (I assume the reason is the same).

I appreciate this question is a hardy perennial. My problem differs from those here and here in that I'm not using JScript, and this one in that I'm not adding the script from the code-behind.

I have used this approach in several other applications and the error does not occur there. Is that path going to pick up the file from the Default.aspx directory? Is there anything I need (in web.config or anywhere else) that I am lacking?

Edit on inspecting the page source (Chrome) the JavaScript in the .aspx file is listed as expected. The included file is shown thus:

<script type="text/javascript" src="MyScript.js"></script>

but no script is shown (ought there to be?)

Community
  • 1
  • 1
Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
  • Is MyScript.js definitely being loaded by the browser? If so, is it the right version? (You may need to do a cache refresh) – Mourndark Dec 24 '15 at 10:51
  • Open Developer Tools in Chrome, and see if reports an error when trying to load `MyScript.js`. Go to the Sources tab and see if `MyScript.js` is listed. – Barmar Dec 24 '15 at 10:51
  • @Barmar, I did as you suggested; the results are in the edit. – Brian Hooper Dec 24 '15 at 10:55
  • If it's not showing the script, it got an error while trying to load it. Go to the Network tab and then reload the page. – Barmar Dec 24 '15 at 10:56
  • Is that the correct path? could it be `src="/MyScript.js"`? If you browse `http://localhost/MyScript.js` is it served? – Davin Tryon Dec 24 '15 at 10:56
  • If the script has been correctly loaded, you see the script in the tree panel under `sources` tab of your Chrome inspector or 'Network' tab of your Firefox inspector. – TaoPR Dec 24 '15 at 10:58
  • The script won't be shown on the page, just the reference to it. Have a look in the Sources tab to see if `MyScript.js` is listed there, and have a look in the Network tab to see if there was an error loading it when the page loaded. – Mourndark Dec 24 '15 at 10:59
  • @DavinTryon, that appears to be the problem. Sorry to have troubled you chaps. But now I wonder why it worked for so long, and why it works in other applications. – Brian Hooper Dec 24 '15 at 11:02
  • `src="MyScript.js"` will work for the *relative* path. Did you recently change the base path? Or are you browsing at something like: `http://localhost/path/`. In that case, the relative `src` will look for the file at `http://localhost/path/MyScript.js` – Davin Tryon Dec 24 '15 at 11:12
  • @DavinTryon, I am activating the application from Visual Studio. Sorry, I should have mentioned this. – Brian Hooper Dec 24 '15 at 11:14

0 Answers0