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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remote scripting lite 1

Status
Not open for further replies.

vongrunt

Programmer
Mar 8, 2004
4,863
HR
Take a look at the following code (crippled, hard-coded, IE 5.5+ only ):

Code:
<HTML XMLNS:MSIE ><MSIE:DOWNLOAD ID="oDL" STYLE="behavior:url(#default#download)" />
<form>
<input type="text" name="zip" onchange="remotecall();">
<span id="zip_name" style="width: 200px; background-color: #eeeeee;"></span>&nbsp;
<input type="submit" name="save data">
</form>

<script language="javascript">
function remotecall()
{	oDL.startDownload( "bar.htm?zip=" + window.event.srcElement.value, returncall );
}

function returncall( s )
{	zip_name.innerText = s;
}
</script>

User types in zipcode, goes somewhere else (simulate that with tab key). Browser passes zipcode to bar.htm server-side but without resubmitting entire page. Whatever/whenever bar.htm returns will be displayed next to zipcode. This concept is also known as "asynchronous remote scripting".

Now, I'm curious is there any simple way to do the same in pure DOM javascript/HTML? No IE stuff, Java/COM, and preferably without IFRAMEs.


 
sure...you can dynamically change the "src" attribute of a <script> tag, and load a new file in it.

e.g.
Code:
<html>
<head>
<script id="oScript"></script>
</head>
<body>
<input type="button" value="load script1" onclick="document.getElementById('oScript').src = 'script1.js';"/>

<input type="button" value="load script2" onclick="document.getElementById('oScript').src = 'script2.js';"/>
</body>
</html>

script1.js:
Code:
alert("script1 loaded");

script2.js:
Code:
alert("script2 loaded");


if you don't want to make a bunch of .js files to load, create one server-side script to load like "zip.php", which would dynamically write the contents.



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Kewl... <script> node never came to my mind. Tnx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top