How to ensure – Kalpen Patel Jul 29 '16 at 04:20

  • download the js file and give the that file path. – naresh vadlakonda Jul 29 '16 at 04:20
  • @KalpenPatel oh you live in terrible misconception – Marko Mackic Jul 29 '16 at 04:20
  • @GyuHyeonChoi It can only point to a local .js file or a .js file online some where like a CDN. Either way if it's pointed to the file correctly whether it's local or not it should load. Is the file local? – tcasey Jul 29 '16 at 04:21
  • @MarkoMackic But it seems like that there can be error while loading script? Sometimes the function I define in JS file is enabled but sometimes it its not. – ghchoi Jul 29 '16 at 04:22
  • @tcasey I'm loading the local script file. I'm serious. It's really happening... – ghchoi Jul 29 '16 at 04:24
  • @MarkoMackic I mean to say if you are putting js in head tag than HTML parse it sequentially. so it will load first. I have referred this here http://stackoverflow.com/questions/28635141/sequence-in-which-html-page-loads – Kalpen Patel Jul 29 '16 at 04:24
  • 1
    What do you mean by "fails sometimes"? Can you include text of `js` at Question? – guest271314 Jul 29 '16 at 04:36
  • @guest271314 because it usually loads the file well. But sometimes, I cannot click the button. I'm using Sign in with Google account API. I define auth2.attachClickHandler in a separated JS file. Usually I can click the button but I cannot click the button somtimes. – ghchoi Jul 29 '16 at 05:51
  • 3 Answers3

    4

    Add a handler for the onerror and/or onload events.

    <script>
        function errorHandler(script) {
            script.src = "backupLib.js";
        }
    </script>
    <script src="someLib.js" onerror="errorHandler(this)"></script>
    
    4castle
    • 32,613
    • 11
    • 69
    • 106
    • is there any reason this isnt more widely used? I've never seen this before but it seems incredibly useful – mcky Jul 29 '16 at 04:24
    • @MichaelMK Probably because most people don't use unstable links. It's more common for `img` tags. – 4castle Jul 29 '16 at 04:26
    • @4castle Would you please explain how can I try to reload when it fails? – ghchoi Jul 29 '16 at 17:09
    • @GyuHyeonChoi In `errorHandler`, change the `src` attribute on the ` – 4castle Jul 29 '16 at 17:15
    • @4castle Thanks! But seriously, my JS file is usually loaded well. Sometimes, onClickHandler I attached in my JS file is not loaded and I cannot invoke click event. So I may try to reload it, right? – ghchoi Jul 29 '16 at 18:03
    • @GyuHyeonChoi Check for errors in your developer console (F12 on windows). If you have errors in your code, that would be different than a 404 error, which is what I thought this was about. – 4castle Jul 29 '16 at 18:14
    0

    Browser developer tools will helps you...

    Load the website in the browser and open the developer tools then network tab, refresh the page.

    There you can see the status of resources, what are all loaded in the page(images,js,css..), If the status code was 200 then it is OK.

    enter image description here

    Sankar
    • 6,908
    • 2
    • 30
    • 53
    -2
    1. Script tag doesn't have an error event handler.
    2. But we can use onload event handler instead like this :

      <script>
          function fileLoaded(name) {
             console.log('JS File loaded # '+ name  );
             alert('JS File loaded # '+ name  );
          }
      
          function fileLoadingError(name) {
             console.log('File error # '+ name  );
          }
      </script>
      <script src=".../jsFile.js" onload="fileLoaded('1')" onerror="fileLoadingError('1')"></script>
      
    3. Make sure to keep the function first.

    4. Even if there's error, the onload method will be called.