Solved

return value from asp page via javascript

Posted on 2001-07-09
24
186 Views
Last Modified: 2012-08-14
I am trying to write an implementation of an API that communicates with an ASP server via Javascript only, returning the results of the call to the function callee.  Herein lies my problem thus far.

The initial way I thought about doing this is as follows:

1. My API implementation is in the topmost parent frame of my page that builds two frames (hidden and content).

2. The content looks for the API in it's parent frame, when it finds it it calls an api.initialize(); function.

3. The api submits a form in the hidden frame and the asp page writes the returned values to the frame.

4. The calling javascript function loops while it checks to see if the values have been returned or not...

if(i < 10000 && typeof(hidden.returnval) != "undefined")
    i++;

that i < 10000 loop will eventually be turned into a timeout with seconds, but for now it just makes it keep checking for the value.

5. When the server writes the new page, the javascript loop ends and the function returns hidden.returnval from the server to the calling function.


Herein lies the problem.  Because javascript does not have a sleep function, I cannot get the function to halt so that the server can refresh the hidden frame.  

However, if I put a alert() call before the loop, it returns on the first try the correct value.  I would assume this is because it causes the function to sleep until you press the okay button, allowing the other frame to refresh.  

REQUIREMENT:  The function call that contacts the server must return a value from the server to the callee.

I have looked extensively for elements relating to this topic, but have come up with nothing, therefore I bring this to you... GREAT 'OL EXPERTS!

BTW: This implementation is not required.  If you can think of any other way to do this WITHOUT JAVA then feel free to mention it.  

TEST CODE I HAVE ASSEMBLED:

*****************< index.htm >************************

<TITLE>main frame</TITLE>
<script>
<!--

function getValue(val){
     
     hidden.document.open();
     hidden.document.write('<html><body><form name=thisform action="server.asp" method=post>');
     hidden.document.write('<input type=hidden name=var1 value="' + val + '">');
     hidden.document.write('</form>');
     hidden.document.write('<script>document.forms.thisform.submit();</script></body></html>');
     hidden.document.close();
     
     var i = 0;
     
     while(i < 10000 && typeof(hidden.var11) == "undefined")     {
          i++;
     }
     
     //alert("i = " + i + "\nvar11 = " + hidden.var11 + "\ntemp = " + temp);
     
     return hidden.var11;

}
// -->
</script>
     
<FRAMESET COLS="275,*">
<FRAME name="hidden" src="about:blank">
<FRAME name="content" src="start.asp">
</FRAMESET>

</HEAD>

<BODY>

</BODY>
</HTML>



*********************< start.asp >*******************

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<a href="javascript:alert(parent.getValue('location'));">Click me</a>
</BODY>
</HTML>

*********************< server.asp >********************

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<%

if Request.Form("var1") <> "" then
     Response.Write(Request.Form("Var1"))
     Response.Write("<SCript>var var11; var11='" &Request.Form("Var1") &"';</script>")
end if

%>
<SCript>var var11; var11='<%=Request.Form("Var1")%>';</script>
ASDF
</BODY>
</HTML>
0
Comment
Question by:punkrider
  • 15
  • 8
24 Comments
 

Author Comment

by:punkrider
ID: 6266098
i will give another 300 points to whoever solves this problem for me.  EE wouldn't let me set the points to 600 like i wanted, so I will post a bogus question and let whoever I choose take the extra 300...

GOOD LUCK
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266141
actually there is a setTimeout function that is very much like a sleep function


timer=setTimeout("doit()", 1000) //do it every second!

anyways, it looks like that should solve your problem, but I will review the script for any other bugs.

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266145
actually...

I mispoke... timer=setTimeout("doit()", 1000 //do it once, in a second...


function doit()
{
alert('hi')
timer=setTimeout("doit()",1000) // do it every second!
}

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266168
also, have you considered using

hidden.window.onload=doit;

function doit()
{
alert(hidden.var11)
}

...I will post both possibilities.

generally the onload will yeild better results than using setTimeout.

Bob

0
 

Author Comment

by:punkrider
ID: 6266174
i tried a set timeout and moved the form writing to that function, but to no avail.  

remember, you cannot have any alert boxes though, because that causes the function to sleep.  it will work no problem if you put an alert box in the code.

keep trying.

-jon
0
 

Author Comment

by:punkrider
ID: 6266191
the only thing that you should rely on to test this is the alert box that displays the return result from the function, as it occurs after the function returns.  

also.. with the onload, the problem here is that in IE and NS both, the page doesn't even refesh until the first javascript function returns.  This causes the onload event to fire after the alert box returns undefined from the function.
0
 
LVL 19

Expert Comment

by:webwoman
ID: 6266302
If you're trying to do an asp function call in client side javascript without reloading the page, it's never going to work -- asp runs SERVER side, not CLIENT side, so it's already finished before the javascript ever starts. And if you've got an onload event, it's going to fire everytime you get something from the server, because the page is going to reload.

Get all the variables from the asp page BEFORE you do anything in javascript, and once you start the javascript function, it has to ALL run on the CLIENT -- no more server side calls at all.

Or just run everything server side. You can't have it both ways.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266329
I am noticing your problem and it is infuriating!!

but I am confident I can come up with a solution (I don't really have anything else to do today :)


Bob


0
 

Author Comment

by:punkrider
ID: 6266379
webwoman,

I am reloading a page.  It is the hidden frame.  This code works if you put in a alert() box to halt the flow of the javascript function for only a second so that the hidden asp frame can refresh itself and write back the new javascript data that i need.

ie:

1. submit form in hidden frame  
2. loop in javascript function until hidden frame refreshes with new content.
3. read value returned from server and return from javascript function with that value.


bebonham,

thanks for your help, I know your pain believe me... You will be greatfully rewarded if you solve this.

-Jon
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266431
I have had success with setTimeout.

I am still looking for the "REAL" answer.

this is the link that will execute it:


start.asp::

<a href="#" onclick="parent.getValue();setTimeout('alert(parent.hidden.dongle)',3000);return false">try this</A>



server.asp::

var dongle=angle




finally:

the main index.html script:

   <SCRIPT>
hidden.location.href='http://www.milestonemortgage.com/cgi-bin/rjs.pl?field1=value1'
</SCRIPT>


that can be seen on my site at http:/www.milestonemortgage.com/Untitled1.htm

and the U is case sensitive.

wait a few seconds, and it will alert ('mangle') which is what I am printing to it via the server
as you can see at
http://www.milestonemortgage.com/cgi-bin/rjs.pl


Bob


hope that helps...

I will try and get it using onload.

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266636
the only issue is....

can I assign it's value to a variable, or do you have to have the value assigned BY the function

ie.

is this okay?

//get the infor
function getValue()
mydata=data
alert(mydata)


or does it have to be

mydata=getValue()


it would help if we knew what you were doing with the value getValue() returns after you get it...


the onload solution I currently have does this:

starts to load the page.
sets the onload for that page to a function in index.html

that function has access to that value now, and you can use it however you want.

HOWEVER.  everything to do with the value HAS to be kicked off by the function referenced by that onload...

now the script at http://www.milestonemortgage.com/Untitled1.htm reflects this technique...

you will notice that there really isn't a delay after clicking the link anmore...it just spits out "mangle"

Bob

0
 

Author Comment

by:punkrider
ID: 6266676
bebonham,

Because I'm programming this as an implementation to an API, the underlying structure that I choose should not be available to the user.  IE: They are not going to have any idea what variable to use once I get the value, so it must be returned from the function.

mydata = getValue('location'); is the only valid way to get data.

If it helps, I am trying to figure out a way to implement the LMS SCORM standard over Javascript and ASP.  In their documentation it offers this as one option for communication, but I'm beginning to think that they didn't think this through enough before offering it as an option.  All code they reference for examples are using Java, and Java Servlets... something I don't want to do if I don't have to.  

-Jon
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Expert Comment

by:bebonham
ID: 6266723
Jon,

I simply used a set value, because it is easier for me...

now that it is working, it is easy to return it to accepting an argument as you shall see.

this corrected code should explain it all

here is the revised test code...

give me your comments:

TEST CODE I HAVE ASSEMBLED:

*****************< index.htm >************************

<TITLE>main frame</TITLE>
<script>
<!--
var result=''
done=false
function getValue(val){
  hidden.location.href="http://www.yoursite/yourscripts/server.asp?var1=" + val
}
function doit()
{
result=hidden.var11
done=true
alert(result + " this is from index.html)
}

function ch()
{
if(done)
{
return(parent.result)
}
else
{
setTimeout(ch(),300)
}
}
// -->
</script>
   
<FRAMESET COLS="275,*">
<FRAME name="hidden" src="about:blank">
<FRAME name="content" src="start.asp">
</FRAMESET>

</HEAD>

<BODY>

</BODY>
</HTML>



*********************< start.asp >*******************

<HTML>
<HEAD>
<TITLE></TITLE>

</HEAD>
<BODY>
<a href="javascript:parent.getValue('location');str=parent.ch();alert(str + ' this is from start.asp')">Click me</a>
</BODY>
</HTML>

*********************< server.asp >********************

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<%

if Request.Form("var1") <> "" then
    Response.Write(Request.Form("Var1"))
    Response.Write("<SCript>var var11; var11='" &Request.Form("Var1") &"';</script>")
end if

%>
<SCript>var var11; var11='<%=Request.Form("Var1")%>';
window.onload=parent.doit
</script>
ASDF
</BODY>
</HTML>




it is also runnning at the http://www.milestonemortgage.com/Untitled1.htm

let me know,

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6266743
a few little typos in the alert statement...



TEST CODE I HAVE ASSEMBLED:

*****************< index.htm >************************

<TITLE>main frame</TITLE>
<script>
<!--
var result=''
done=false
function getValue(val){
 hidden.location.href="http://www.yoursite/yourscripts/server.asp?var1=" + val
}
function doit()
{
result=hidden.var11
done=true
alert(result + " this is from index.html")
}

function ch()
{
if(done)
{
return result
}
else
{
setTimeout(ch(),300)
}
}
// -->
</script>
   
<FRAMESET COLS="275,*">
<FRAME name="hidden" src="about:blank">
<FRAME name="content" src="start.asp">
</FRAMESET>

</HEAD>

<BODY>

</BODY>
</HTML>



*********************< start.asp >*******************

<HTML>
<HEAD>
<TITLE></TITLE>

</HEAD>
<BODY>
<a href="javascript:parent.getValue('location');str=parent.ch();alert(str + ' this is from start.asp')">Click
me</a>
</BODY>
</HTML>

*********************< server.asp >********************

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<%

if Request.Form("var1") <> "" then
   Response.Write(Request.Form("Var1"))
   Response.Write("<SCript>var var11; var11='" &Request.Form("Var1") &"';</script>")
end if

%>
<SCript>var var11; var11='<%=Request.Form("Var1")%>';
window.onload=parent.doit
</script>
ASDF
</BODY>
</HTML>



good luck,

Bob
0
 

Author Comment

by:punkrider
ID: 6267146
bob,

I have already thought about what you have done.  The problem with this, though, is that the function that calls the actual getValue() function does not get a return value.

function getValue(val){
hidden.location.href="http://www.yoursite/yourscripts/server.asp?var1=" + val

/********************
 Need to be able to return value here, otherwise you lose the callee.
********************/
}


The problem is that my final implementation will be as follows:

function GoNext(){

var api = getAPI();

var nextLink = api.getValue('cmi.course.nextpart');

content.document.location.href = nextLink;

}

Because I'm doing this from an API, there is no way that I can use the parent frame at all.  I can't even use any functions other than those already specified by the API outside of the API.  I am not going to be building the content, as that is built by others in a standard fashion (SCORM) which utilizes the standardized API calls.  

so ... at any given moment, the user must be able to write a piece of content with the expectation that calling the function api.getValue('next.location'); will return them the value of the next location.

If I am misunderstanding your solution in any way, please correct me.

Thanks for your help today!

-Jon
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6267249
is this not acceptable?

api.getValue('cmi.course.nextpart');
var nextLink=ch();
content.document.location.href = nextLink;

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6267291
it seems that the first function returns without waiting for a timeout or the onload

you will have to wait for the onload to assign the varible.

do you get this?

I don't think you have a choice, but lv it open, I am still looking.
0
 

Author Comment

by:punkrider
ID: 6267412
Bob,

api.getValue('cmi.course.nextpart');
var nextLink=ch();
content.document.location.href = nextLink;

is not acceptable for my api to pass as SCORM standards compliant, sorry.  The reason that I posted this question with so high points is that the real problem is returning a value from within that same function to the callee.

What I am looking for is a way to "sleep" that function so that the hidden frame can refresh, so that i can check for the return value until it arrives and then return it from within my function.

After many hours of trying, I have not been able to figure anything out that works as of yet.  If you go back to my original example and put an alert() right before the while loop you will notice that the return value correctly appears...wierd.  It seems that I am having more of a threading problem than anything.  I just wish that the browser frames would act independently, but I don't think that is possible anymore.

Thanks,

-Jon



0
 
LVL 8

Expert Comment

by:bebonham
ID: 6267428
so you want something clean?

then I can't use a setInterval before it, or while around it?
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6267437
if that is the case, then you have found that:

1. there is a new(to me) bug in javascript.
...but it can be overcome...with a workaround, so I am hesitant.


2. that SCORM standard is not possible.

since I am from javascript, I will blame SCORM.

bye.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6267455
this?

http://xml.coverpages.org/scorm.html

does it really specify such a thing?

where, I am curious?

Bob
0
 
LVL 8

Accepted Solution

by:
bebonham earned 300 total points
ID: 6267461
but you are right too,

it IS a threading problem now that I think about it.
0
 

Author Comment

by:punkrider
ID: 6267632
Thanks for your help...I hope you at least learned something today, even if it wasn't good!

page 51 of <a href="http://www.adlnet.org/Scorm/docs/SCORM_2.pdf">this</a> document shows an example implementation with Javascript and ASP.  I'm thinking that now they didn't actually think it through because I don't think it is possible, thus the reason that all of the examples are in Java ;)

0
 
LVL 8

Expert Comment

by:bebonham
ID: 6269187
Jon,


thanks for the points..

here's what I found in that document :

check out page 53, it seems like they are wanting to use javascript & java together...

you make the applet and then connect to it using javascript (live connect?)

I am not sure...I think that would only work in netscape...right?

anyway, that example shows using js only as a method of letting content people communicate with your applet without having to know java.

also, maybe more importantly to you I notice this:

p 123:

they say that implementations of the javascript is not important, and that it is outside the standards of SCORM

...but I may misunderstand that...



the technique we were working on has to make a trip to a server to work...

so I don't really think the problem is with javascript>..

I think that if they say you can use CGI and ASP (which they do)

they are wrong, because that requires multiple server trips.

the idea here is that you have to have an applet or a activex control running on the client that can have all the data dumped into it.


I don't know what to tell you...

I think we have found this

javascript will not wait for a function to return inside a function.
--it will run through a function and evaluate everything, and if it is not "ready" yet, it is returning undef.



I think the best way to go with this is as they suggest ... with an applet or activex that communicates directly with the Lesson.

that way the activex/applet can get the data in the client memory, so it can be accessed without a server trip.



Frustrating for you, I imagine,

good luck, I am curious to know how you proceede.

Bob
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

746 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