Reusing a script

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

    Reusing a script

    I have a function sililar to the one below on one of my pages. It puts
    the value of a field on a form into a variable called FieldName:

    function checkSomething( ) {
    var FieldName = document.frmFor mName.FieldName .value
    }

    I'd like to reuse the function on a different page (instead of writing
    another almost identical script on that page), but I need to change the
    name of the field in the script depending on the page that the script is
    used on.

    How do I change the script so that I can put the script in an includes
    file and then pass the name of the field to the script according to the
    page that the script is being used?


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Fred Oz

    #2
    Re: Reusing a script

    Luis wrote:[color=blue]
    > I have a function sililar to the one below on one of my pages. It puts
    > the value of a field on a form into a variable called FieldName:
    >
    > function checkSomething( ) {
    > var FieldName = document.frmFor mName.FieldName .value
    > }
    >
    > I'd like to reuse the function on a different page (instead of writing
    > another almost identical script on that page), but I need to change the
    > name of the field in the script depending on the page that the script is
    > used on.
    >
    > How do I change the script so that I can put the script in an includes
    > file and then pass the name of the field to the script according to the
    > page that the script is being used?
    >[/color]

    You can pass a reference to something rather than hard code the
    reference. Below some code that is fairly self explainatory.

    You could also pass the name or id of something as a string, then use
    something like:

    function checkSomething( x) {
    var theThing = document.getEle mentById(x);
    ....

    ... onclick="checkS omething('theFo rm');"....


    Cheers, Fred,



    It shows how to pass a reference to 'this',

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head><title>Re use Script</title>
    <script type="text/javascript">
    function checkSomething( a) {
    var msg = 'You passed: ' + a;
    if (a == '') msg += 'an empty string';
    if (a.nodeName) msg += '\n Node name: ' +a.nodeName;
    if (a.type) msg += '\n Type: ' + a.type;
    if (a.id) msg += '\n ID: ' + a.id;
    if (a.name) msg += '\n NAME: ' + a.name;
    if (a.value) msg += '\n VALUE: ' + a.value;
    alert(msg);
    }
    </script>
    </head>
    <body>
    <div id="aDiv" onclick="checkS omething(this); "
    style="padding: 10px 30px 10px 30px; background-color: #99cccc;
    width: 200px; height 40px;">A clickable Div</div>
    <form action="" name="theForm">
    <input type="text" name="aTextInpu t" size="50"><br>
    <input type="button" name="aButton" value="Pass the form"
    onclick="checkS omething(this.f orm);">
    <input type="button" name="aButton" value="Pass the 'text' input"
    onclick="checkS omething(this.f orm.aTextInput) ;">
    <input type="button" name="aButton"
    value="Pass the 'text' input value"
    onclick="checkS omething(this.f orm.aTextInput. value);">
    <input type="reset" value="Reset">
    </form>
    </body>
    </html>

    Comment

    • Dr John Stockton

      #3
      Re: Reusing a script

      JRS: In article <41760c87$0$248 99$c397aba@news .newsgroups.ws> , dated
      Wed, 20 Oct 2004 06:58:15, seen in news:comp.lang. javascript, Luis
      <andyzaNOSPAM@w ebmail.co.za> posted :[color=blue]
      >
      >How do I change the script so that I can put the script in an includes
      >file and then pass the name of the field to the script according to the
      >page that the script is being used?[/color]

      Reading <URL:http://www.merlyn.demo n.co.uk/js-nclds.htm> should answer
      most of your needs.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...