0

I'm studying to make Web application.

It seems like the script tag

<script src="...js"></script>

fails sometimes.

I've tried to find how to solve it but I cannot find the exact solution yet.

How can I ensure script tag successfully load Javascript?

Thank you for your help in advance.

11
  • It can't fail, if file is online and accessible Commented Jul 29, 2016 at 4:18
  • @Marko Mackic really? What if the file is not online?
    – ghchoi
    Commented Jul 29, 2016 at 4:19
  • whenever your page start loading in browser than it already first load all javascripts than all html code.why yo need to ensure that <script> is loaded or not? Commented Jul 29, 2016 at 4:20
  • download the js file and give the that file path. Commented Jul 29, 2016 at 4:20
  • 1
    What do you mean by "fails sometimes"? Can you include text of js at Question? Commented Jul 29, 2016 at 4:36

3 Answers 3

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>
6
  • is there any reason this isnt more widely used? I've never seen this before but it seems incredibly useful
    – mcky
    Commented Jul 29, 2016 at 4:24
  • @MichaelMK Probably because most people don't use unstable links. It's more common for img tags.
    – 4castle
    Commented Jul 29, 2016 at 4:26
  • @4castle Would you please explain how can I try to reload when it fails?
    – ghchoi
    Commented Jul 29, 2016 at 17:09
  • @GyuHyeonChoi In errorHandler, change the src attribute on the <script> tag to load something else. Reloading a broken link wouldn't be helpful.
    – 4castle
    Commented Jul 29, 2016 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
    Commented Jul 29, 2016 at 18:03
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

-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.

1
  • I'm not sure what your sources are. #1 & #4 are wrong. Run your code to find out.
    – 4castle
    Commented Jul 29, 2016 at 4:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.