Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call Javascript from ASP?

Status
Not open for further replies.

Lbob

Programmer
May 23, 2003
157
GB
I'm trying to call a piece of javascript within some asp and failing!

I've got the following javascript function in the head
function select_month(iMonth)
{
document.fSelected.tMonth.value = iMonth
}

Then when the page is loaded and the dates are all configured I want the month to be put in a text field for processing

so I have the following..
if Request.QueryString("sDate") = "" then
sDate = customdateformat(Date(),"-")
else
sDate = Request.QueryString("sDate")
end if

iMonth = Month(sDate)

<----PASS iMonth TO JAVASCRIPT

Can anyone help?
Thanks
Lbob
 
You could just Response.Write the value of your ASP variable straight into the javascript variable. Since the ASP script executes server-side and the javascript executes client-side, the javascript doesn't ever have to know if your assigning a static value to te variable or if the value came from an ASP variable:
Code:
<%
Dim thisMonth
thisMonth = Month(Now)
%>
<script language=&quot;javascript&quot;>
var aMonth;
aMonth = <%=thisMonth%>;
</script>

You could just as easily call a function with that value:
Code:
<%
Dim thisMonth
thisMonth = Month(Now)
%>
<script language=&quot;javascript&quot;>
select_month(<%=thisMonth%>);
</script>

at the same time, though, I don't see why you couldn't just write that month into your selection rather than relying on javascript:
Code:
Dim i, tMonth
tMonth = Month(Now)
Response.Write &quot;<select name=&quot;&quot;selMonth&quot;&quot;>&quot;
For i = 1 to 12
   Response.Write &quot;<option value=&quot;&quot;&quot; & i & &quot;&quot;&quot;&quot;
   If i = tMonth Then Response.Write &quot; selected&quot;
   Response.Write &quot;>&quot; & MonthName(i) & &quot;</option>&quot;
Next

In this case I am looping from 1 to 12 to display the month names, using the number of the month as the value for my options. In each case I check to see if the month is equal to this month, if it is I add the word selected into the opening option tag to make that the default selection.

This has the added plus that it should be browser independant because it doesn't rely on javascript to do any of the work, so it won't matter what version or type of browser they are using.

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 

select_month(&quot;<%=sDate%>&quot;)

function select_month(iMonth)
{
document.fSelected.tMonth.value = iMonth
}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
OK Tarwn, how'd you write all of that while I was writing my one liner? [ponder]

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
You mean yo don't have a personal black hole generator that allows you to nminutely bend time in your location in order to increase the speed of your typing? :p

Yes, as a amatter of fact I am on my first cup today and drinking bad coffee at that :p

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
you should see him when he has good coffee!

speaking of, when are you going to get up and leave that machine for some good coffee? [lol]

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
Heh, I have to mail out a harddrive and pay some bills today...maybe I'll stop for some good coffee along the way :)

or not, the only coffee in my imediate area s Starbucks, Port City Java (our local version of Starbucks), and Barnes and Noble (starbucks coffee with any other name...)

Maybe I'll just get off my lazy behind and hit the supermarket :p That way you waon't have to hear me complain about my coffee days on end :)

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
[tt]I have IBM's time machine, but have to figure out how it works...



Delete * from brain Where MaxLevel = &quot;Full&quot; and reaction = &quot;Slow&quot; order by StartOver
 
Thanks for your help guys I ended up using the following
%>
<script type=&quot;text/javascript&quot; language=&quot;JavaScript&quot;>
select_month(<%=iMonth%>);
</script>
<%


LBOB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top