Click to See Complete Forum and Search --> : javascript methods not available with asp?


BYS2
07-06-2006, 07:29 PM
i am currently making a website with asp and i am having a little problem. i am using VBscript for the asp page but at some point in the code i want to call a javascript function (i am better with javascript). so here is some sample code:

<html>
<head>
<title>hi</title>
</head>
<body>

<script language='javascript' runat='server'>
function getFirstLetter(text)
{
var display = text.substring(0,1)
response.write(display)
}
</script>

<%
Dim test
test = "this is a test"
call getFirstLetter(test)
%>

</body>
</html>

the function is just suppoesd to get the first letter of the word and print it out but for some reason it does not work.. but when i replace the javascript function with a vbscript version:

sub getFirstLetter (text)
var display = left(text,1)
response.write(display)
end sub

then it works fine..

i don't know why my javascript function won't run... does server-side javascript not have these basic string methods?

buntine
07-07-2006, 02:43 AM
You are mixing server-side and client-side technologies. The very concept of Server-Side Programming forbids this.

Read the sticky thread I have created in this forum to get a better idea of why you cannot do this.

To resolve your problem, you simply need to write a VBScript version of the function:

Function GetFirstLetter(strText)
GetFirstLetter = Left(strText, 1)
End Function

Cheers.

BYS2
07-07-2006, 08:52 AM
hmm ok... but can't asp be written with javascript? at least that's what the w3c tutorials say... according to what i understand, you should be able to use all the objects and methods of the core javascript language (no DOM) when programming asp pages using javascript.. furthermore, the w3c tutorial site also has examples of an asp page written in vbscript which at some point calls a javascript function... i thought all that you needed was to include a "runat='server'" attribute to the <script> tag for the javascript to run server-side



EDIT: acutally, the code that i provided above does work.. lol... i didn't bother checking before i posted because i thought it woudln't.. the problem was that in the actual site, i am pulling the name of someone out of a database and passing that into the getFirstLetter function. but i forgot that i was in fact passing an object... so i was doing: <% call getFirstLetter(rs1("name")) %> instead of doing <% call first(cstr(rs1("name"))) %>.. in other words, i forgot to convert type and therefore the function didn't work.. problem solved.. lol