Solved

asp in .js file

Posted on 2007-11-23
4
657 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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…

744 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

18 Experts available now in Live!

Get 1:1 Help Now