none
Accessing A Javascript Script Using Code From an HTML Page RRS feed

  • Question

  •      I was doing a tutorial video which had me create a page and place code in it to prompt an alert function script using Javascript.  The first set of page code seen directly below worked out fine.  The alert "Hello, this is really working!" popped up when I previewed the page.  The second set of page code, is similar, but you will see that it calls a Javascript script file named "custom.js" for it to do exactly what the first page did, but in this case the actual code is in the called file.  When using the second set of page code, when the page was previewed it did not show the alert "Hello, this is really working!" which it is supposed to be doing.  I would appreciate any feedback on this.  All three code sets are below.  Please note that I did not include the information above the <head> area such as the DOCTYPE declaration and opening HTML tag in the 2 sets of page code (HTML pages) below because when I try to submit the thread it does not allow me because of live links.  In those two actual pages that information is there though and has not been tampered with.  As I mentioned the script is contained in a Javascript script file named "custom.js".  This was all done with Expression Web 4.

    First set of page code is below   

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>

    <script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="js/jquery-migrate-1.1.1.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    alert("Hello, this is really working!");

    });

    </script>

    </head>

    <body>
    </body>

    </html>

    This is the second set of page code, same as above but it calls a javascript called "custom.js" from the js folder instead of having the actual script written in the head area.

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>

    <script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="js/jquery-migrate-1.1.1.js"></script>
    <script type="text/javascript" src="js/custom.js"></script>

    </head>

    <body>
    </body>

    </html>

    Below is the code that is being used in the "custom.js" file (javascript script file) which is being called by the second set of page code.  Please note that this script code is identical to the script code that was in the first set of page code but in a separate file.     

    <script type="text/javascript">

    $(document).ready(function(){

    alert("Hello, this is really working!");

    });

    </script>





    • Edited by C_Jones11 Saturday, February 23, 2013 2:32 AM
    Saturday, February 23, 2013 2:18 AM

Answers

  • In your "custom.js" file, remove the "<script type="text/javascript">" and "</script>" wrappers. Those are HTML script elements, and should not be present in a javascript (js) file. Only the javascript should be in such a file. It should contain only this:

    $(document).ready(function(){

    alert("Hello, this is really working!");

    });

    Give that a try... -)

    cheers,
    scott


    Please remember to "Mark as Answer" the responses that resolved your issue. It is common courtesy to recognize those who have helped you, and it also makes it easier for visitors to find the resolution later.

    • Marked as answer by C_Jones11 Saturday, February 23, 2013 8:42 PM
    Saturday, February 23, 2013 2:59 AM

All replies

  • In your "custom.js" file, remove the "<script type="text/javascript">" and "</script>" wrappers. Those are HTML script elements, and should not be present in a javascript (js) file. Only the javascript should be in such a file. It should contain only this:

    $(document).ready(function(){

    alert("Hello, this is really working!");

    });

    Give that a try... -)

    cheers,
    scott


    Please remember to "Mark as Answer" the responses that resolved your issue. It is common courtesy to recognize those who have helped you, and it also makes it easier for visitors to find the resolution later.

    • Marked as answer by C_Jones11 Saturday, February 23, 2013 8:42 PM
    Saturday, February 23, 2013 2:59 AM
  •      It worked perfectly.  That was all I needed.  Thanks a lot Scott.

    Casey

    Saturday, February 23, 2013 8:42 PM