[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

asp in .js file

Posted on 2007-11-23
4
Medium Priority
?
665 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 600 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 400 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 600 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

650 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