Call a javascript function from ASP page

M

Mark

I've searched a bunch of groups, and they say this can be done, but for
some reason, I can't get it to work.

Here's my ASP page:

<% @ Language=VBScript %>
<Script language=javascript>
function cont(strTitle){
if("undefined"!=typeof strTitle){
window.opener.location='somepage.asp?title=' + strTitle;
window.close();
}
else
window.close();
}
</script>

<%
strTitle=Upload.form("title")
Do Stuff

cont(strTitle)
%>

<body ONLOAD="cont(<%=strTitle%>">
</body>

What I want to happen, is if this page gets called from a form and
title is null or undefined, then close the window. Otherwise, do
something, then close the window with the javascript call.

Thanks,

- Mark
 
L

Larry Bud

Mark said:
I've searched a bunch of groups, and they say this can be done, but for
some reason, I can't get it to work.

Here's my ASP page:

<% @ Language=VBScript %>
<Script language=javascript>
function cont(strTitle){
if("undefined"!=typeof strTitle){
window.opener.location='somepage.asp?title=' + strTitle;
window.close();
}
else
window.close();
}
</script>

<%
strTitle=Upload.form("title")
Do Stuff

cont(strTitle)
%>

<body ONLOAD="cont(<%=strTitle%>">
</body>

What I want to happen, is if this page gets called from a form and
title is null or undefined, then close the window. Otherwise, do
something, then close the window with the javascript call.

You're missing a closing paren ) in the onload
 
M

Mark

Good eye, but sadly, that's just my typing the code into the post, not
in my code.

Thanks.
 
A

Aaron Bertrand [SQL Server MVP]

Good eye, but sadly, that's just my typing the code into the post, not
in my code.

Another error I spotted is that I assume strTitle should have quotes around
it. So, it should be

<body onload="cont('<%=strTitle%>');">

(Assuming strTitle can't have ' in it, if so, then

<body onload="cont('<%=Replace(strTitle, "'", "\'")%>');">

But since you didn't tell us what "doesn't work" means, I have no idea if
I'm wasting my time looking at the syntax or if I should focus on your
approach (because you can't call cont() -- a client-side JavaScript
function -- from ASP, which runs on the server, not on the client).

It would be helpful if you better define "can't get it to work". Are you
getting an error message? If so, what is it? Or, how is it not working the
way you would expect? Is the window not launching the right page, or is it
only launching when the title is empty instead of the opposite, or is it
always opening, or is it never opening?

Have you considered this approach instead?

<script language=javascript>
function checkit()
{
<%
strTitle=Upload.form("title")
' Do Stuff

if strTitle <> "" then
%>
window.opener.location.href = 'somepage.asp?title=<%=strTitle%>';
<% end if %>
window.close();
}
</script>
<body onload="checkit();">
 
M

Mark

The issue is, as you say, calling client-side java with ASP.

Your approach looks very promicing. I'm going to give it a shot.

Thanks,

- Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top