Click to See Complete Forum and Search --> : javascript - some problems


akgalp
October 20th, 2005, 12:44 AM
hello all

i was experimenting some javascript as shown below


1.asp

<%
rec = "select count(*) from error_log "
set rs_rec = session("conn").execute(rec)
Response.Write ("var cnt="&rs_rec(0)&";")
rs_rec.close
set rs_rec=nothing

rec = "select id from error_log "
set rs_rec = session("conn").execute(rec)

while not rs_rec.eof
str = str & rs_rec(0) & ","
rs_rec.movenext
wend
Response.Write ("var recs='"&str&"';")
rs_rec.close
set rs_rec=nothing

%>

display();
function display()
{
var i;
var sel1=document.getElementById("select1");
sel1.options.length=0;
var rec = recs.split(",");
for(i=0 ; i < rec.length ; i++)
{
sel1.options[i] = new Option(rec[i],rec[i],false,false);
}
}

test.asp

<%@language=vbscript%>
<HTML>
<HEAD>
</HEAD>
<script language='javascript'>
function fill_list()
{
var tab = document.createElement('script');
tab.setAttribute('language','javascript');
tab.setAttribute('id','scr1');
tab.setAttribute('src','./1.asp');
document.getElementsByTagName('body')[0].appendChild(tab);
}

</script>

<BODY>
<%
set session("conn")=server.CreateObject("ADODB.connection")
session("conn").connectionstring="DSN=testdata;UID=test;pwd=test"
session("conn").open

%>
<SELECT style="WIDTH: 181px; HEIGHT: 361px" size=2 id='select1' name='select1'>
<OPTION></OPTION>
</SELECT>
<INPUT id='button1' onclick='javascript:fill_list()' type='button' value='click' name='button1'>
</body>
</html>




what i was trying to do is to fetch an asp file from the server dynamicaly , without refreshing the client side screen..

ie the test.asp will display a listbox and a button at the client side. when the button is clicked it loads 1.asp from server using javascript DOM manipulation. It works for the first time only . afterwards the same data will be presented even if the data in the database changes.. How to force the browser to reload 1.asp from server


thanks
akg

PeejAvery
October 20th, 2005, 12:59 AM
what i was trying to do is to fetch an asp file from the server dynamicaly , without refreshing the client side screen..
Your problem with this is that ASP is interpreted on the server side before it even reaches the client window. There is no way to execute server side code unless you hide the ASP page in an IFRAME. You can then use this hidden IFRAME to pass information back to the server and after execution refresh the main page.

<iframe width=0 height=0 marginwidth=0 marginheight=0 frameborder=0 name="framename" src="about:blank"></iframe>

akgalp
October 20th, 2005, 02:15 AM
i didn't understand .. can you explain a little more .. any links/docs would be great

thanks for the replay
akg

PeejAvery
October 20th, 2005, 11:07 AM
What I am saying is you can't pass information with ASP without refreshing. ASP is interpreted before it reaches the client. Therefore you must refresh so that the server sees the new information.

What you can do is use an IFRAME, as coded below. A JavaScript function can trigger this IFRAME with the following information.

Ex.
http://www.website.com/index.php?info=getthisinfo.
<iframe width=0 height=0 marginwidth=0 marginheight=0 frameborder=0 name="framename" src="about:blank"></iframe>
Since the IFRAME is hidden yet loading this information, the server will see it and interpret it. Then pass those variables back to the main page with JavaScript.

Ex.
<script language="JavaScript">
parent.formname.objectname.value = blahblahblah;
</script>
This will refesh the page yet not allow the viewer to see the refresh process. It will act as though it is updating in realtime.

akgalp
October 21st, 2005, 12:41 AM
Ok..thanks..for the info

akgalp
October 21st, 2005, 03:41 AM
ok..one more problem observed..
i found a tut on net abt iframe
http://developer.apple.com/internet/webcontent/iframe.html

and i was trying to do some code using iframe and form
here's what i had done
client.html


<script language='javascript'>
function handleResponse(str)
{
alert(str);
}

var iframe;
function create(formname)
{
try{
var srvf = 'server.asp'+buildQueryString(formname);
var temp=document.createElement('iframe');
temp.setAttribute('id','newframe');
temp.setAttribute('name','newframe');
temp.style.height='0px';
temp.style.width='0px';
temp.style.border='0px';
iframe=document.getElementsByTagName('body').item(0).appendChild(temp);
iframe.setAttribute('src',srvf);
}
catch(e)
{
}

}

function buildQueryString(theFormName) {
theForm = document.forms[theFormName];
var qs = ''
for (e=0;e<theForm.elements.length;e++) {
if (theForm.elements[e].name!='') {
qs+=(qs=='')?'?':'&'
qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
}
}
return qs
}


</script>

<body>

<form name="emailForm" id="emailForm" action="server.asp" onsubmit='javascript:create(this.name)'>
Your name:<br>
<input type="text" name="realname"><br>
Your message:<br>
<textarea name="message" cols="50" rows="10"></textarea><br><br>
<input type="submit" value="submit">
</form>

</body>



Server.asp

<%@language='vbscript'%>
<% response.expires=0 %>
<script language="javascript">
var name = '<%=request.querystring("realname")%>';
window.parent.handleResponse('hello '+name);
window.parent.handleResponse('your message is \n '+ '<%=request.querystring("message")%>');
</script>


if in this is run .. the response is as expected .. ie it shows two messageboxes , after this IE shows a script error a 'object doesn't support this prperty or method' at line


'window.parent.handleResponse('hello '+name);'

in the server.asp .. why ??

now if i change the
<input type="submit" value="submit"> to
<input type="button" value="submit" onclick='javascript:create("emailForm")' >

and the
<form name="emailForm" id="emailForm" action="server.asp" onsubmit='javascript:create(this.name)'>

to

<form name="emailForm" id="emailForm" action="server.asp" >

.. it works fine ..

is there any problem in the code ??
thanks

PeejAvery
October 21st, 2005, 01:49 PM
I am wondering why you are building an IFRAME with JavaScript instead of just adding the tags to the HTML?

akgalp
October 22nd, 2005, 01:15 AM
if done inside the script .. as far i have seen . the browser back and fw buttons will be available.. so if the server.html is loaded and hitting refresh again loads server.html ..

if done in js the refresh will load the client.html only and the fw and back buttons have no effect

thanks