Hi Moondaddy,
Thanks for your response. Yes, you're right, when use the <script
src=".....js" ></script> block to link a js file, if multi pages has
referenced the same file( make sure that the absolute path on the server
are also same if you're using relative path to specify the js file in
multipages). When the first time the js file is referenced. It'll be
downloaded to the clientside browser's cache. Then, if the same page or
even other pages refernce the same js file, the browser will check the
file's url path and find the file has already been referenced so it'll
retrieve it from the client's browser cache rather than request it again
from the serverside. Is this feature what you want?
Also, here is a tech artile in MSDN which has discussed the similar
siutaion to this:
#Creating a Web Part with Client-side Script
http://msdn.microsoft.com/library/en...lientScript.as
p?frame=true
In addtion, since the browser will always use the cached file if you hard
code the js file's path such as
<script language=javasc ript src=common.js ></script>. So there'll cause
some trouble if the js file on the serverside has been changed and need
update(request again from the serverside). Here is a solution to it:
You can append a "version" or date information without defeating the point
of
cached .js files.
<script language="javas cript" src="common.js? version=2004011 51300"></script>
When file.js is modified, simply change the value of "version" to something
else. Then, since the browser find that the link src's value has changed,
it'll request the file again from serverside. Thus, you can dynamically
determine whether to update the js from serverside or still use the file in
cache. How do you think of this?
Also, I 've made a generic sample page to show the above means, here is the
page code;
---------------------------aspx page--------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DynamicJ S</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body onload="PopupMe ssage()">
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
<asp:TextBox id="txtJSVersio n" runat="server"> </asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button id="btnPostBack " runat="server" Text="Post
Back"></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>
---------------------code behind class---------------------------
public class DynamicJS : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Text Box txtJSVersion;
protected System.Web.UI.W ebControls.Butt on btnPostBack;
protected string scriptblock = "<script language='javas cript'
src='common.js? version={0}' ></script>";
private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack)
{
txtJSVersion.Te xt = DateTime.Now.To LongTimeString( );
string sb = String.Format(s criptblock,txtJ SVersion.Text);
Page.RegisterSt artupScript("dy namicscript",sb );
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.btnPostBac k.Click += new
System.EventHan dler(this.btnPo stBack_Click);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void btnPostBack_Cli ck(object sender, System.EventArg s e)
{
string sb = String.Format(s criptblock,txtJ SVersion.Text);
Page.RegisterSt artupScript("dy namicscript",sb );
}
}
---------------------------the "common.js" file-----------------------------
function PopupMessage()
{
alert("Hello World!");
}
---------------------------------
In this sample page, I put a textbox on the page to let user input a
version number, when the version number remains the same, the page will
always use the common.js in the client browser's cache. If we change the
value in the txtbox, it'll request the linked "common.js" from serverside
again. You can try modifying the common.js in half and unchange the
txtbox's value and post back the page, you'll find the popup message
remains the old. If you change the txtbox's value , then post back again,
the message will get changed(which means it update the js file in client
cache from serverside).
Please try out the preceding suggestions. If you feel anything unclear,
please feel free to let me know.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)