Solved

asp in .js file

Posted on 2007-11-23
4
658 Views
Last Modified: 2010-04-21
hi there.
i have a form and retrieves some functions from .js file. what i'm trying to do is make the radio button checked if the value is 1.

my current code is

txtHTML = '<table><tr><td class="style19">Caching Server required</td><td class="style19"><input id="optCacheServer" name="optCacheServer" type="radio" value="1"  onChange="setRec();">Yes<input id="optCacheServer" name="optCacheServer" type="radio" value="0" onChange="setRec();" >No</td></tr></table>';


what i would like to do is actually
txtHTML = '<table><tr><td class="style19">Caching Server required</td><td class="style19"><input id="optCacheServer" name="optCacheServer" type="radio" value="1" <% if CSR2 = 1 then %> checked <% end if %> onChange="setRec();">Yes<input id="optCacheServer" name="optCacheServer" type="radio" value="0" <% if CSR2 = 0 then %> checked <% end if %> onChange="setRec();" >No</td></tr></table>';

however this doesnt work as it doesnt seem to accept asp codes. any ideas how i can achieve it ? thanks in advance
0
Comment
Question by:cempasha
4 Comments
 
LVL 2

Accepted Solution

by:
DavidBlackledge earned 150 total points
ID: 20341570
I'm no asp expert, but I know jsp, and I'm sure it's the same issue...
a .js file is a .js file, not a .asp file, so the compiler doesn't attempt to compile your .js file.  
You can either:
move your javascript content directly into your .asp file so it will process as you want (probably the easiest thing to do)
or
use the .js file as an include (however asp does that) in your page, causing it to be interpreted as part of the .asp page  (something like <script><%@include file="myfile.js"%></script>),
or finally you could do the complicated solution of making your .js file a .asp file, but changing the headers so the browser knows it's really supposed to be type=text/javascript and it does the right thing with it.

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 100 total points
ID: 20342220
Or alternatively do this in the asp page:

<script>
function initPage() {
var CachingServerRequired =<%= CSR2 %>;
document.getElementById('optCacheServer')[((CachingServerRequired)?0:1)].click();
}
window.onload=initPage;
</script>

And you should NOT use onChange but onClick:

txtHTML = '<table><tr><td class="style19">Caching Server required</td><td class="style19"><input id="optCacheServer" name="optCacheServer" type="radio" value="1"  onClick="setRec();">Yes<input id="optCacheServer" name="optCacheServer" type="radio" value="0" onClick="setRec();" >No</td></tr></table>';

Open in new window

0
 
LVL 29

Assisted Solution

by:Göran Andersson
Göran Andersson earned 150 total points
ID: 20342723
It's possible to use asp code in a javascript file by making it an asp file, as DavidBlackledge suggests, but that will not help you at all in this situation. The browser caches the javacript files (even if you specify that it shouldn't), so the code will only run once, then the code will not change.

You can make code that creates a Javascript variable with the value from your asp variable:

<script type="text/javascript">
var CSR2 = <%=CSR2%>;
</script>

Then you can use the Javacsript variable in the function:

txtHTML = '<table><tr><td class="style19">Caching Server required</td><td class="style19"><input id="optCacheServer" name="optCacheServer" type="radio" value="1" ' + (CSR2 == 1 ? 'checked ':'') + 'onChange="setRec();">Yes<input id="optCacheServer" name="optCacheServer" type="radio" value="0" ' + (CSR2 == 0 ? 'checked ' : '') + 'onChange="setRec();" >No</td></tr></table>';
0
 
LVL 5

Author Closing Comment

by:cempasha
ID: 31410730
thanks for your help. i've ended up using

-- move your javascript content directly into your .asp file so it will process as you want (probably the easiest thing to do)

comment. however all seems great. thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now