Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can javascript load server data after initial page load 1

Status
Not open for further replies.

webscripter

Programmer
Jul 29, 2002
266
US
I have a script that I would like to write in js. But I would have to be able to load txt data or mysql data several times after the page has loaded without reloading the whole page. Can JavaScript do this?

Thanks
Pat
ideas@microwebber.com
 
Yup... the common cross-browser trick is to use HTML element with "src" attribute (hidden IFRAME or SCRIPT) as "proxy", plus some kind of callback mechanism that informs document when loading is finished (onload event).

For IE-only, see "download" behavior.
 
Do you have a cut and paste example of this?
It would be great if I could load js files. Then use the loaded variables.

just off the top of my head will this work in most browsers?
will this alone do the trick
function loadVars(myscript){
document.write('<SCRIPT src="'+myscript+'"></SCRIPT>')
}



Thanks
Pat
ideas@microwebber.com
 
Nope - document.write will erase entire document. Here is very basic example:
Code:
<script language="javascript" id="proxy"></script>
<form>
<textarea name="test" cols=60 rows=10></textarea>
<input type="button" value="Script 1" onclick="loadScript('script1.js')">
</form>

<script language="javascript">
var iCnt = 1;
function loadScript( sFile )
{	var oScr = document.getElementById("proxy");
	[b]oScr.src = sFile ;[/b]
}
</script>
See line in bold - there you'll probably have to set absolute URL. This is sample code for script1.js:
Code:
document.forms[0]["test"].value += "\nScript 1 loaded";
Now, instead of script.js point to PHP file that generates javascript.

I remember some problems with earlier mozillas and SCRIPT nodes... IFRAME should work everywhere.
 
I have this code in an html file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Button Code</title>
<script language="javascript" id="proxy"></script>
</head><body>
<form>
<textarea name="test" cols=60 rows=10></textarea>
<input type="button" value="Script 1" onclick="loadScript('</form>
<script language="javascript">
loadScript("function loadScript( sFile )
{ var oScr = document.getElementById("proxy");
oScr.src = sFile ;
}
</script>
</body>
</html>

and this code in the js file loading.js
document.forms[0]["test"].value += "\nScript 1 loaded";


It works great in IE, but not in Netscape

Thanks
Pat
ideas@microwebber.com
 
I tried IFRAME instead of SCRIPT and it causes a download prompt in IE.

I guess I don't need it to function in Mozilla.

Thanks
Pat
ideas@microwebber.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top