...

View Full Version : Nested Java Scripts or passing values.



Nasith
07-29-2002, 05:48 PM
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?

ShriekForth
07-29-2002, 06:28 PM
It seems that IE will allow this, parse it correctly anyway.




<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

brothercake
07-29-2002, 06:30 PM
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.

adios
07-29-2002, 07:21 PM
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.

Nasith
07-29-2002, 07:41 PM
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.

ShriekForth
07-30-2002, 04:03 PM
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

Nasith
07-30-2002, 04:10 PM
If they want it that bad they can have it. :p

Nothing there they can hurt or hurt me with.

Thanks for the tip.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum