Call a javascript function from ASP page

Discussion in 'ASP General' started by Mark, Jul 27, 2006.

  1. Mark

    Mark Guest

    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
     
    Mark, Jul 27, 2006
    #1
    1. Advertisements

  2. Mark

    Larry Bud Guest

    You're missing a closing paren ) in the onload
     
    Larry Bud, Jul 27, 2006
    #2
    1. Advertisements

  3. Mark

    Mark Guest

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

    Thanks.
     
    Mark, Jul 27, 2006
    #3
  4. Good eye, but sadly, that's just my typing the code into the post, not
    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();">
     
    Aaron Bertrand [SQL Server MVP], Jul 27, 2006
    #4
  5. Mark

    Mark Guest

    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
     
    Mark, Jul 27, 2006
    #5
  6. Mark

    Mark Guest

    Works great. Thanks for the instruction.

    - Mark
     
    Mark, Jul 27, 2006
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.