Solved

asp in .js file

Posted on 2007-11-23
4
660 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

830 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