Click to See Complete Forum and Search --> : JS Design help


Timg
11-06-2003, 12:19 PM
Did a search for this and came up empty. Any thoughts, suggestions, and/or advice appreciated!

Currently I am in the process of redesigning our web app into an MVC compliant application (java based). I'm working on the 'view' piece at the moment (everything else is essentially done) and have come a cross what I'll term the 'Page Specific Code' issue.

My original goal was to eliminate all javascript from specific pages. For the most part this had been successful. However I've run into a stumbling block.

We have numerous places in the app where one of the following conditions are true.

1) The value of input field x directly affects the 'enablement' of another input field y either based upon the value of x (if select x has a value of a, select y enables) or a criteria such as length (if the length of text x exceeds a, text y enables).


var acctVal = document.skcs400in.in_accounta.value;
if(acctVal.length >= 9) {
document.skcs400in.in_accountb.selectedIndex = 0;
document.skcs400in.in_accountb.disabled=true;
}
else {
document.skcs400in.in_accountb.disabled=false;
}


2) The value of field x dictates whether field y is required or not. (if field x contains a, field y is required, otherwise it's not)

if (selectedValue == 'N') {
document.forms.SPTRD400in.in_new_department.required = true;
document.forms.SPTRD400in.in_cusip.required = true;
}
else {
document.forms.SPTRD400in.in_new_department.required = false;
document.forms.SPTRD400in.in_cusip.required = false;
}

3) The changing the value of field x to a specific value resets the value of field y

var rate = document.REPO100in.in_rate.value.charAt(0).toUpperCase();
if (rate == 'E') {
document.REPO100in.in_rate.value = " ";
}

These JS pieces all have to do with manipulating form fields based upon input in other form fields on the same page (without submission to the server of course).

My question is does anyone know of a good way to centralize these types functions to allow the pages themselves to be JS free (just using a .js file)?

96turnerri
11-06-2003, 03:26 PM
so you want a link to a .js file with no reference for it to function???

Timg
11-06-2003, 04:10 PM
I'm looking to genericise page specific code so that I can move the code out of the particular jsp and in to a .js file without hard coding specific values, field names, and/or ids.

The more I think about it however, the more it seems as though the task requires writing some sort or rule engine for js. Which of course I'm not keen on doing.

96turnerri
11-06-2003, 05:20 PM
hm sounds ominous, however i have no experience with .jsp so im afraid i cant help you there, i tried lol

Timg
11-06-2003, 05:36 PM
Ominous indeed. However the code I'm concerned about centralizing is JavaScript thus the post in this forum. JSP, ASP, and PHP are all very similar in terms of their mixing server side and client side code.

My goal is to eliminate having JavaScript spread over hundreds of pages and becoming a maintenance nightmare (with replication of functionality, etc).

I've gotten everything moved to other areas of our application except those JavaScript blocks that have dependencies on specific fields and those field's values. By necessity, those blocks still reside in specific pages.

So to restate the issue...

How do you turn page specific JavaScript into JavaScript that lives in a .js file and still provides the same features I listed in the original post (without writing a rules engine)?

Any takers?

96turnerri
11-06-2003, 07:05 PM
i think your just asking this in a complicated way

<body>
<script language="javascript" src="JS/XXXX.js"></script>
<a href="javascript:openFunction()">Function Name</a>

obviously changed the function bit to what you need it to do

Timg
11-07-2003, 09:33 AM
I'm sorry but obviously my question does not make too much sense to you (probably my fault).

I'm going to repost this in a reformulated way...

96turnerri
11-07-2003, 09:34 AM
ok lol mite be able to help you then :D