Basic Question on calling javascripts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JCO

    Basic Question on calling javascripts

    I have several JavaScripts in a web page. Each script is in an individual
    file with extension .js.
    I want to know if it is possible to put all functions in one single page.
    If so, how do I call the specific script that I want to run.

    This is how I call an individual script:
    <SCRIPT LANGUAGE="JavaS cript" src="valForm.js "></SCRIPT>

    Where valForm is a script that looks like this:
    function valForm()
    {
    if( document.frmPas sword.txtInput. value == "password" )
    document.locati on.href="Privat e.htm";
    else
    document.locati on.href="NoAcce ssPage.htm";
    }


    If a single script contains 5-10 functions, how do you call the specific
    function you want?

    Thanks in advance.



  • Dennis M. Marks

    #2
    Re: Basic Question on calling javascripts

    I have read the following message from " JCO" <J.Oliviero@ver izon.net>
    and have decided to lend my vast knowledge.

    The writer said:[color=blue]
    > I have several JavaScripts in a web page. Each script is in an individual
    > file with extension .js.
    > I want to know if it is possible to put all functions in one single page.
    > If so, how do I call the specific script that I want to run.
    >
    > This is how I call an individual script:
    > <SCRIPT LANGUAGE="JavaS cript" src="valForm.js "></SCRIPT>
    >
    > Where valForm is a script that looks like this:
    > function valForm()
    > {
    > if( document.frmPas sword.txtInput. value == "password" )
    > document.locati on.href="Privat e.htm";
    > else
    > document.locati on.href="NoAcce ssPage.htm";
    > }
    >
    >
    > If a single script contains 5-10 functions, how do you call the specific
    > function you want?
    >
    > Thanks in advance.
    >[/color]
    and my reply is:
    The name of a js file has nothing to do with the name of the function
    inside. In fact a script may not even contain a function. You can one
    or multiple js files depending on how you use them. The browser loads
    them all into the page and does't care where they came from. Just name
    them anything you want.
    function xxx() {
    ....
    };
    function yyy() {
    ....
    };

    function zzz() {
    ....
    };
    Put the above in 1, 2, or 3 js files. It doesn't matter.
    To use a function just refer to it's name
    xxx() or yyy() or zzz()

    --
    Dennis M. Marks

    Replace domain.invalid with dcsi.net


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

    Comment

    • McKirahan

      #3
      Re: Basic Question on calling javascripts

      " JCO" <J.Oliviero@ver izon.net> wrote in message
      news:m2dWb.8258 $%X3.6077@nwrdd c01.gnilink.net ...[color=blue]
      > I have several JavaScripts in a web page. Each script is in an individual
      > file with extension .js.
      > I want to know if it is possible to put all functions in one single page.
      > If so, how do I call the specific script that I want to run.
      >
      > This is how I call an individual script:
      > <SCRIPT LANGUAGE="JavaS cript" src="valForm.js "></SCRIPT>
      >
      > Where valForm is a script that looks like this:
      > function valForm()
      > {
      > if( document.frmPas sword.txtInput. value == "password" )
      > document.locati on.href="Privat e.htm";
      > else
      > document.locati on.href="NoAcce ssPage.htm";
      > }
      >
      >
      > If a single script contains 5-10 functions, how do you call the specific
      > function you want?
      >
      > Thanks in advance.[/color]

      Just merge them into a single include file and run it.

      Nothing else has to change unless, perhaps, if there are duplicate function
      names or global variables declared.


      Functions are called in numerous ways:

      <body onload="func1() ">

      <form onsubmit="retur n func2()">

      <img src="button.gif " onclick="func3( )">

      and, of course, from within other functions.


      Does this help?


      Comment

      • Lee

        #4
        Re: Basic Question on calling javascripts

        JCO said:[color=blue]
        >
        >I have several JavaScripts in a web page. Each script is in an individual
        >file with extension .js.
        >I want to know if it is possible to put all functions in one single page.
        >If so, how do I call the specific script that I want to run.[/color]

        Your terminology is likely to cause confusion at some point.
        A program written in C++ is not "a C++".
        A script written in JavaScript is not "a JavaScript".

        [color=blue]
        >This is how I call an individual script:
        ><SCRIPT LANGUAGE="JavaS cript" src="valForm.js "></SCRIPT>[/color]

        That's not really "calling" anything. That's telling the browser
        where to load a block of JavaScript from. The browser loads the
        code block into the current page, making the functions available
        and executing any in-line code they may contain.

        [color=blue]
        >Where valForm is a script that looks like this:
        >function valForm()
        >{
        > if( document.frmPas sword.txtInput. value == "password" )
        > document.locati on.href="Privat e.htm";
        > else
        > document.locati on.href="NoAcce ssPage.htm";
        >}
        >
        >
        >If a single script contains 5-10 functions, how do you call the specific
        >function you want?[/color]

        The only way to call a function is to invoke it by name,
        as in "valform()" . It doesn't matter where the browser
        loads it from. It's either loaded into the current page,
        or it isn't.

        Comment

        • JCO

          #5
          Re: Basic Question on calling javascripts

          Thanks all three for your reply. I guess I was getting confused because in
          my example, the name of the js script is also the name of the file source.
          Now I realized that the source file is loaded one time which makes all of
          the functions available to my website.

          Very cool! Thanks again


          "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
          news:c0bnud0ktu @drn.newsguy.co m...[color=blue]
          > JCO said:[color=green]
          > >
          > >I have several JavaScripts in a web page. Each script is in an[/color][/color]
          individual[color=blue][color=green]
          > >file with extension .js.
          > >I want to know if it is possible to put all functions in one single page.
          > >If so, how do I call the specific script that I want to run.[/color]
          >
          > Your terminology is likely to cause confusion at some point.
          > A program written in C++ is not "a C++".
          > A script written in JavaScript is not "a JavaScript".
          >
          >[color=green]
          > >This is how I call an individual script:
          > ><SCRIPT LANGUAGE="JavaS cript" src="valForm.js "></SCRIPT>[/color]
          >
          > That's not really "calling" anything. That's telling the browser
          > where to load a block of JavaScript from. The browser loads the
          > code block into the current page, making the functions available
          > and executing any in-line code they may contain.
          >
          >[color=green]
          > >Where valForm is a script that looks like this:
          > >function valForm()
          > >{
          > > if( document.frmPas sword.txtInput. value == "password" )
          > > document.locati on.href="Privat e.htm";
          > > else
          > > document.locati on.href="NoAcce ssPage.htm";
          > >}
          > >
          > >
          > >If a single script contains 5-10 functions, how do you call the specific
          > >function you want?[/color]
          >
          > The only way to call a function is to invoke it by name,
          > as in "valform()" . It doesn't matter where the browser
          > loads it from. It's either loaded into the current page,
          > or it isn't.
          >[/color]


          Comment

          • Bas Cost Budde

            #6
            Re: Basic Question on calling javascripts

            I recently learned that language="javas cript" is deprecated in favor of
            type="text/javascript"

            Just a remark.
            --
            Bas Cost Budde

            but the domain is nl

            Comment

            • JCO

              #7
              Re: Basic Question on calling javascripts

              Yes I have noticed that too. I made the changes.
              However, can you tell me the difference in the code below:

              <FORM NAME="frmPasswo rd" onSubmit="retur n valForm()">
              <FORM NAME="frmPasswo rd" onEnter="return valForm()">

              "Bas Cost Budde" <bas@heuveltop. org> wrote in message
              news:c0ftgm$qnm $9@news2.solcon .nl...[color=blue]
              > I recently learned that language="javas cript" is deprecated in favor of
              > type="text/javascript"
              >
              > Just a remark.
              > --
              > Bas Cost Budde
              > http://www.heuveltop.org/BasCB
              > but the domain is nl
              >[/color]


              Comment

              • Bas Cost Budde

                #8
                Re: Basic Question on calling javascripts

                JCO wrote:
                [color=blue]
                > However, can you tell me the difference in the code below:
                >
                > <FORM NAME="frmPasswo rd" onSubmit="retur n valForm()">
                > <FORM NAME="frmPasswo rd" onEnter="return valForm()">[/color]

                Er, the first line uses the Submit event? That shall not be your question.

                (reads the HTML 4.01 spec) I can't find an onEnter property. What is
                that supposed to mean?

                --
                Bas Cost Budde

                but the domain is nl

                Comment

                Working...