Solved

Call Javascript from ASP?

Posted on 2012-03-22
32
467 Views
Last Modified: 2012-06-27
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
Comment
Question by:Ahelbling
  • 15
  • 12
  • 5
32 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37754382
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37754418
That was C#.

VB would be:

ClientScript.RegisterClientScriptBlock(Me.GetType(), "myScript", "approvalCheck()", True)
0
 

Author Comment

by:Ahelbling
ID: 37754432
that works in classic ASP?
0
 

Author Comment

by:Ahelbling
ID: 37754460
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754484
this is classic asp, not .net, ignore all ClientScript.RegisterClientScriptBlock(...) thing for now...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754496
is approvalCheck on the same iFrame/frame/page with that code?
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37754497
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754499
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
 

Author Comment

by:Ahelbling
ID: 37754504
IE = yes
0
 

Author Comment

by:Ahelbling
ID: 37754516
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754524
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754529
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
 

Author Comment

by:Ahelbling
ID: 37754534
Yes, I get the message using that...
0
 

Author Comment

by:Ahelbling
ID: 37754552
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
 

Author Comment

by:Ahelbling
ID: 37754558
and this with my original code:

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

<html>
<head>
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754559
ok, on that page can you see the code for approvalCheck(...) function which you posted on your first message...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Ahelbling
ID: 37754564
yes, beat you to the punch ;)  (for once)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754566
looks like you added the function to top frame and trying to call it from another page which is in an iframe
0
 

Author Comment

by:Ahelbling
ID: 37754570
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37754572
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754578
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
 

Author Comment

by:Ahelbling
ID: 37754580
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
 

Author Comment

by:Ahelbling
ID: 37754589
I am using frames not iframe  - TOP and BOTTOM.  All of the above code has come from BOTTOM.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754595
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
 

Author Comment

by:Ahelbling
ID: 37754596
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
 

Author Comment

by:Ahelbling
ID: 37754604
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
 
LVL 51

Accepted Solution

by:
HainKurt earned 400 total points
ID: 37754607
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754617
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
 

Author Comment

by:Ahelbling
ID: 37754621
I understand. thank you.  Let me test that last suggestion.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37754629
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
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 100 total points
ID: 37754636
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
 

Author Comment

by:Ahelbling
ID: 37754647
Thank you both.  I now understand my wrongdoing and have learned from my mistakes.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now