Call a javascript function from ASP page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    Call a javascript function from ASP page

    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=VBScri pt %>
    <Script language=javasc ript>
    function cont(strTitle){
    if("undefined"! =typeof strTitle){
    window.opener.l ocation='somepa ge.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

  • Larry Bud

    #2
    Re: Call a javascript function from ASP page


    Mark wrote:
    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=VBScri pt %>
    <Script language=javasc ript>
    function cont(strTitle){
    if("undefined"! =typeof strTitle){
    window.opener.l ocation='somepa ge.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

    Comment

    • Mark

      #3
      Re: Call a javascript function from ASP page

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

      Thanks.

      Comment

      • Aaron Bertrand [SQL Server MVP]

        #4
        Re: Call a javascript function from ASP page

        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(strTi tle, "'", "\'")%>');" >

        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=javasc ript>
        function checkit()
        {
        <%
        strTitle=Upload .form("title")
        ' Do Stuff

        if strTitle <"" then
        %>
        window.opener.l ocation.href = 'somepage.asp?t itle=<%=strTitl e%>';
        <% end if %>
        window.close();
        }
        </script>
        <body onload="checkit ();">


        Comment

        • Mark

          #5
          Re: Call a javascript function from ASP page

          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

          Comment

          • Mark

            #6
            Re: Call a javascript function from ASP page

            Works great. Thanks for the instruction.

            - Mark

            Comment

            Working...