Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Call Javascript from ASP?

Can someone please help me to understand why this is not working for me?  I am trying to call a javascript to reload the top frame and also change a button's color in that top frame based on the results of a SQL query.  The response.write("Approved") works, so I know the query is working.

sSQL = "select ExpID from Trans where LogID = " & nLogID & "AND ExpID = 0"
set rsApproved = oConn.Execute(sSQL)
    if NOT IsObject(rsApproved) then
        response.write ("Approved")
        Response.Write "<script type='text/javascript'>approvalCheck();</script>"
    else
        response.write ("NOT Approved")
        Response.Write "<script type='text/javascript'>approvalCheck();</script>"
    end if
        

%>

<html>
<head>

Open in new window

<script type="text/javascript">
    function approvalCheck() {
        var frm1 = window.top.frames[0];
        var a = frm1.document.getElementById('Approval')
        a.style.backgroundColor = "#FFFFFF"; 
        frm1.location.reload();
    }
</script>

</head>

Open in new window

SS
0
Ahelbling
Asked:
Ahelbling
  • 15
  • 12
  • 5
2 Solutions
 
Tom BeckCommented:
Response.Write will paste the script block to the top of the rendered html. It cannot run from there. Use ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript", "approvalCheck()", true);
0
 
Tom BeckCommented:
That was C#.

VB would be:

ClientScript.RegisterClientScriptBlock(Me.GetType(), "myScript", "approvalCheck()", True)
0
 
AhelblingAuthor Commented:
that works in classic ASP?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AhelblingAuthor Commented:
Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/corpcardnew/EditCurrentNew2.asp, line 201

ClientScript.RegisterClientScriptBlock(Me.GetType(), "myScript", "approvalCheck()", True)
-----------------------------------------------------------------------------------------^
0
 
HainKurtSr. System AnalystCommented:
this is classic asp, not .net, ignore all ClientScript.RegisterClientScriptBlock(...) thing for now...
0
 
HainKurtSr. System AnalystCommented:
is approvalCheck on the same iFrame/frame/page with that code?
0
 
Tom BeckCommented:
Sorry, I missed that part of the question.

In that case, isn't the syntax wrong in your script? Should it be in parenthesis?

Response.Write( "<script type='text/javascript'>approvalCheck();</script>")
0
 
HainKurtSr. System AnalystCommented:
are you using IE to test? if yes, go to tools - internet options - advanced and uncheck disable script debugging (IE)
then you will see the error...
0
 
AhelblingAuthor Commented:
IE = yes
0
 
AhelblingAuthor Commented:
I see this error now using:

sSQL = "select ExpID from Trans where LogID = " & nLogID & "AND ExpID = 0"
set rsApproved = oConn.Execute(sSQL)
    if NOT IsObject(rsApproved) then
        response.write ("Approved")
        Response.Write ("<script type='text/javascript'>approvalCheck();</script>")        
    else
        response.write ("NOT Approved")
        Response.Write ("<script type='text/javascript'>approvalCheck();</script>")        
    end if

Open in new window

Line: 1
Error: The value of the property 'approvalCheck' is null or undefined, not a Function object
0
 
HainKurtSr. System AnalystCommented:
try this

        Response.Write ("<script type='text/javascript'>approvalCheck();</script>")        
>>>
        Response.Write ("<script type='text/javascript'>alert('not approved');</script>")

do you get message?
0
 
HainKurtSr. System AnalystCommented:
if message works, but not your function, it means that script is on another page, not on current page or not included or missing somehow...
0
 
AhelblingAuthor Commented:
Yes, I get the message using that...
0
 
AhelblingAuthor Commented:
I see this in the html view of the rendered page:

Complete <br />DeptChange <br />NOT Approved<script type='text/javascript'>alert('not approved');</script>

<html>
<head>
0
 
AhelblingAuthor Commented:
and this with my original code:

Complete <br />DeptChange <br />NOT Approved<script type='text/javascript'>approvalCheck();</script>

<html>
<head>
0
 
HainKurtSr. System AnalystCommented:
ok, on that page can you see the code for approvalCheck(...) function which you posted on your first message...
0
 
AhelblingAuthor Commented:
yes, beat you to the punch ;)  (for once)
0
 
HainKurtSr. System AnalystCommented:
looks like you added the function to top frame and trying to call it from another page which is in an iframe
0
 
AhelblingAuthor Commented:
this shows in the rendered html:

<script type="text/javascript">
    function approvalCheck() {
        var frm1 = window.top.frames[0];
        var a = frm1.document.getElementById('Approval')
        a.style.backgroundColor = "#FFFFFF";
        frm1.location.reload();
    }
</script>

</head>
0
 
Tom BeckCommented:
You cannot call a function that is not on the page yet. The response.write is putting the script block above the <html> tag.
0
 
HainKurtSr. System AnalystCommented:
are you using any iframe?

do you see the code @ 37754570 & 37754558 on the same view source?

or you post sources from different windows each time and confuses us?
0
 
AhelblingAuthor Commented:
I am trying to check the status of the records in the top frame every time the record detail loads in the bottom frame, then change the color of the Approval button in that top frame  depending on that SQL query.  Does that make sense?  I dont want to reload the top frame all the time because I lose my current record indication highlight.
0
 
AhelblingAuthor Commented:
I am using frames not iframe  - TOP and BOTTOM.  All of the above code has come from BOTTOM.
0
 
HainKurtSr. System AnalystCommented:
if you are calling the same script all the time, just add it to the end of your page without any condition...

Response.Write ("<script type='text/javascript'>approvalCheck();</script>")

or use asp tags

...
%>
<script type='text/javascript'>approvalCheck();</script>

or add it to your body

<body onLoad="approvalCheck()">

will you call this function all the time? becasue from your code in both cases you call the same function...
0
 
AhelblingAuthor Commented:
I am sorry I am sure there are 10 way better ways to do most of this, but this is what I have pieced together thus far on this project.
0
 
AhelblingAuthor Commented:
the script is only to run based on the outcome of this:

sSQL = "select ExpID from Trans where LogID = " & nLogID & "AND ExpID = 0"
set rsApproved = oConn.Execute(sSQL)
    if NOT IsObject(rsApproved) then
        response.write ("Approved")
        Response.Write ("<script type='text/javascript'>approvalCheck();</script>")        
        'Response.Write ("<script type='text/javascript'>alert('not approved');</script>") 
    else
        response.write ("NOT Approved")        
    end if

Open in new window

0
 
HainKurtSr. System AnalystCommented:
then do this

dim startupScript

if NOT IsObject(rsApproved) then
        startupScript = "nonApprovalCheck()"
else
        startupScript = "approvalCheck()"
end if

just used 2 different function here, because using the same function does not make any sense...

then use:

<body onLoad="<%=startupScript %>">

or just before </body> tag use

...
<script type='text/javascript'><%=startupScript %></script>
</body>
</html>
0
 
HainKurtSr. System AnalystCommented:
don't use Response.Write in your code, because you run the code you posted on top of your page, even before the <html><head><body> tags...
0
 
AhelblingAuthor Commented:
I understand. thank you.  Let me test that last suggestion.
0
 
HainKurtSr. System AnalystCommented:
or if your structure is like this it works:

<html>
<head>
...
<script type="text/javascript">
    function approvalCheck() {
        var frm1 = window.top.frames[0];
        var a = frm1.document.getElementById('Approval')
        a.style.backgroundColor = "#FFFFFF"; 
        frm1.location.reload();
    }
</script>
</head>
<body>
...
<%
sSQL = "select ExpID...
if NOT IsObject(rsApproved) then
...
else
  Response.Write ("<script type='text/javascript'>approvalCheck();</script>")
end if
%>
...
</body>
</html>

Open in new window

0
 
Tom BeckCommented:
don't use Response.Write in your code, because you run the code you posted on top of your page, even before the <html><head><body> tags...
Please note: I've said that twice already. I was also first to point out that the Response.Write lacked parenthesis.
0
 
AhelblingAuthor Commented:
Thank you both.  I now understand my wrongdoing and have learned from my mistakes.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 15
  • 12
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now