Solved

asp in .js file

Posted on 2007-11-23
4
661 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

738 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