Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Nested Java Scripts or passing values.

    Is it possible to nest javascripts?

    (ie)

    <script language="javascript>
    --script stuff--
    <script language="javascript>
    --script stuff--
    </script>
    </script>


    also

    Is it possible to pass values from an embedded javascript to an external javascript and then pass results back?

    If so how?

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Western US
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It seems that IE will allow this, parse it correctly anyway.

    Code:
    <script language="JavaScript"  type="text/javascript">
    <!--
    function itemTotal(item){
    	doc = "document.frmOrder";
    	eval ("document.frmOrder.txtGrand"+item+".value = document.frmOrder.txtCost" + item +".value * document.frmOrder.txtSold"+ item + ".value");
    
    }
    <script language="JavaScript"  type="text/javascript">
    function calculate(what) {
        for (var i=1,answer=0;i<9;i++)
            answer += what.elements['txtSold' + i].value - 0;
        what.answer.value = answer;
    }
    //-->
    </script>
    </script>
    But....

    Why would you nest them? You could have one function pass the value, or reference to another function. All of them could be in the same script reference on the page. Or in any other script reference on the page. If you declare the variable outside of the function, you would in effect make it global to any other script on the page.

    ShriekForth

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You cannot put nest script tags; it may work in IE, but lots of strange things work in IE.

    The bottom line is this - if you declare a global variable in an external js file, then later scripts on the same page can read and modify that variable, as ShriekForth said.

    My view is that it's best to always declare all variables globally, unless you know 100% it won't be needed later, but some may disagree with that.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Is it possible to pass values from an embedded javascript to an external javascript and then pass results back?
    Nasith:

    For any given window, there is only one scripting environment (area of memory where JavaScript/JScript runs). There is (imo) no such animal as an 'external javascript' - it's just stored externally from the .html file (document) it runs in. It has to be loaded into memory to do anything; once it is, the JS runtime engine hasn't the foggiest notion (so to speak) of where it came from. 'External' scripts are simply read into the .html file at the spot where the <script src="..."></script> container is - exactly as if they were 'embedded' there. All you've done is extract the code for external storage, usually to take advantage of caching for multiple-page use.

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks for the help, one more question.

    For a simple password protect script could you use an ebmedded script to get usernames and passwords from a form and pass the values to an external script for validation then pass back a correct or incorrect value to the embedded script to either open the protected page or display an error? I just need simple way to hide the validation part. As it is simply viewing source will reveal usernames and passwords.

    There is nothing critical being protected just a way to discourage casual users.

    If there is a better way to do this I'm open to any suggestions.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    Western US
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    adios has the easiest way to stop a casual observer with the external javascript.

    <script language="JavaScript" src='./validate.js'>
    </script>

    That would stop anyone from viewing the source with a single right click, but they could still download the .js, or view it in their cache. If anyone really wants to know what the password is, they will find it if it's clientside.

    ShriekForth

  • #7
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If they want it that bad they can have it.

    Nothing there they can hurt or hurt me with.

    Thanks for the tip.


  •  

    Posting Permissions

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