www.webdeveloper.com
Results 1 to 4 of 4

Thread: Javascript src not found

  1. #1
    Join Date
    May 2014
    Posts
    2

    Javascript src not found

    Excuse me if this question is too elementary.

    I don't know what the correct syntax is for HTML5 or if this even works the way I remember, but I was of the understanding that if the source file pointed to by the src attribute of the script tag was not found, the code between the tags is run instead. For instance:

    <SCRIPT type="text/ecmascript" SRC="/script/jquery.js">
    <--
    alert(this.src + " was not included.");
    -->
    </SCRIPT>

    If "/script/jquery.js" is not found this code will pop up an alert box that reads "jquery.js was not included". Is this understanding correct and does it apply to HTML5? Thanks.

  2. #2
    Join Date
    Mar 2011
    Posts
    1,160
    No, your code won't work. You should use something like:
    Code:
    <script src="/script/jquery.js"></script>
    <script>
    if (typeof jQuery == 'undefined') {  
    alert("jQuery was not included.");
    }
    </script>
    to test for the presence of jQuery.

    But as long as you're loading jQuery from a local source, the issue should never come up. If you're having trouble loading jQuery, it probably means the 'src' attribute is malformed. Since you're using relative addressing in your example, make sure that the file exists on the server and then try using a complete URL instead (ie. 'http://www.example.com/script/jquery.js') in the 'src' attribute.

  3. #3
    Join Date
    May 2014
    Posts
    2
    I believe this solution is specific to JQuery. Is there a general solution for detecting the inclusion of .js file?

  4. #4
    Join Date
    Nov 2010
    Posts
    1,102
    you can put an error listener on the script tag
    Code:
    <script src="myscript.js" onerror="alert('no script found')" type="text/javascript"></script>
    which has decent (but not universal) application across browsers (see http://www.quirksmode.org/dom/events/error.html)

    otherwise you could do something hacky like
    Code:
    <script>
    var loaded=false;
    function checkLoad(){
    if (loaded){
    alert("loaded")
    } else {
    alert("not loaded")
    }
    }
    
    </script>
    <script src="myscript.js" onload="loaded=true" type="text/javascript"></script>
    <script>
    checkLoad();
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles