locked
New to html and scripting - can javascript function in one <script></script> be called in another <script></script> RRS feed

  • Question

  • using IE 7, asp pages.  Most of the scripts in our system are vbscripts and are not using the html script tag but <% %>. I'm in the process of learning JavaScript. If I create a function in one html script tag, can the function be used in another html script tag?

    i.e.

    <script> function DisplayMessage(sMessage) { document.write(sMessage); } </script>

    <script> call DisplayMessage("yo!!") </script>

     

    i've tried this with poor results. it does work as:

    <script> function DisplayMessage(sMessage) { document.write(sMessage); } 

     call DisplayMessage("yo!!")

    </script>

    does all processing for a <script></script> have to be contained within that script? 

    If so, how do you create and use javascript libraries of common code? (like error handling etc.) 

     


    Peter schenk
    Tuesday, April 5, 2011 7:49 PM

Answers

  • Hi Peter,

    please check it. Hope this helps.

     

    Code Snippet:

    <script type="text/javascript" language="javascript">        

            function CallAlert()

            {

                ShowAlertMsg();

            }

    </script>    

    <script type="text/javascript" language="javascript">

            function ShowAlertMsg()

            {

                alert("Hey i'm in ShowAlertMsg() Fns");

            }

    </script>

    • Marked as answer by pete2059 Wednesday, April 6, 2011 2:03 PM
    Wednesday, April 6, 2011 3:08 AM

All replies

  • Hi Peter,

    please check it. Hope this helps.

     

    Code Snippet:

    <script type="text/javascript" language="javascript">        

            function CallAlert()

            {

                ShowAlertMsg();

            }

    </script>    

    <script type="text/javascript" language="javascript">

            function ShowAlertMsg()

            {

                alert("Hey i'm in ShowAlertMsg() Fns");

            }

    </script>

    • Marked as answer by pete2059 Wednesday, April 6, 2011 2:03 PM
    Wednesday, April 6, 2011 3:08 AM
  • hi Mr Kaufman.

    senility must be setting in early, I added the code to a simple html and it still doesn't work as two separate scripts. It does as one script.  Please look it over and see what I am doing incorrectly. The code I tested is:

    <html>

    <head>

    <title></title>

    </head>

    <body>
    <script type="text/javascript" language="javascript">       
     CallAlert();
            function CallAlert()

            {

                ShowAlertMsg();

            }
    </script>

    <script type="text/javascript" language="javascript">       

            function ShowAlertMsg()

            {

                alert("Hey i'm in ShowAlertMsg() Fns");

            }

    </script>


    </body>

    </html>

    does the ShowAlertMsg script have to be before the other script or needs to be in the <head> area? as you can tell i'm a little lost as to why this is not working.


    Peter schenk
    Wednesday, April 6, 2011 1:08 PM
  • I see what i'm doing wrong now. this worked: Thanks Mr Kaufman

    <html>

    <head>

    <title></title>
    <script type="text/javascript" language="javascript">       
            function CallAlert()

            {

                ShowAlertMsg();

            }
    </script>
    <script type="text/javascript" language="javascript">       

            function ShowAlertMsg()

            {

                alert("Hey i'm in ShowAlertMsg() Fns");

            }

    </script>

    </head>

    <body>
    <script type="text/javascript" language="javascript">       
     CallAlert();
    </script>

     

    </body>

    </html>


    Peter schenk
    Wednesday, April 6, 2011 2:02 PM
  • Hi Peter,

     i hope you are trying to call "CallAlert()" Function in Page Load event, can you try to modify the <Body> tag as below. hope this will help you to simplify your code.

     

    <body onload="Javascript:CallAlert();">

    Thanks Peter!!!
    Wednesday, April 6, 2011 3:26 PM