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-us/spptsdk/html/CreateWPClientScript.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=javascript 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="javascript" src="common.js?version=200401151300"></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>DynamicJS</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body onload="PopupMessage()">
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
<asp:TextBox id="txtJSVersion" 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.Page
{
protected System.Web.UI.WebControls.TextBox txtJSVersion;
protected System.Web.UI.WebControls.Button btnPostBack;
protected string scriptblock = "<script language='javascript'
src='common.js?version={0}' ></script>";
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
txtJSVersion.Text = DateTime.Now.ToLongTimeString();
string sb = String.Format(scriptblock,txtJSVersion.Text);
Page.RegisterStartupScript("dynamicscript",sb);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnPostBack.Click += new
System.EventHandler(this.btnPostBack_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnPostBack_Click(object sender, System.EventArgs e)
{
string sb = String.Format(scriptblock,txtJSVersion.Text);
Page.RegisterStartupScript("dynamicscript",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.)