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

Make HTML script tag run at server?

Status
Not open for further replies.

tcstom

Programmer
Aug 22, 2003
235
GB
I want to access the 'script' tags in the HTML HEAD section of my page from the server but adding the instruction runat="server" to them seems to cause all sorts of strange behaviour. For example, simply adding runat="server" to a single script tag on the login page for one of my apps gives the error:

Code:
Compiler limit exceeded: Line cannot exceed 2046 characters

Removing that attribute solves the problem. So how do I access script tags from the server?
 
By the way, I tried adding the language="javascript" attribute to the same script tag and received a different error:

Code:
Cannot use 'javascript' because another language has been specified earlier in this page

All I want is to access the script tag from the server as an HtmlGenericControl and modify it's src attribute... there must be a way!!
 
I could do that but my intention is to keep this generic. I don't want to have to write out all the Javascript declarations for each page from the server. What I want to do is just run a generic method for each page that locates all script tags and appends a date querystring to the src attribute. So if I make a Javascript change I can instruct the server to update this querystring which would force clients to update their cached scripts. I had thought this was a simple idea but obviously not. Any other ideas welcome...
 
Can you provide an example of what your script tags look like to start with?

I can't test anything at the moment but I'll try to have a look at it later.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Nothing special, just like this:

Code:
<script src="scripts/jquery.js" type="text/javascript"></script>

What I want is to change this on the server (using a date stored in my database) to this:

Code:
<script src="scripts/jquery.js?updated=2008-01-31" type="text/javascript"></script>

But adding the runat="server" instruction to the tag causes the problems described above. Your help is appreciated...
 
Actually, if you add runat="server" to a script tag, I wonder if it would try to read the javascript code and fall over there?

Try removing all the javascript from the file, save it and then try to recompile. If it compiles, then it means that you won't be able to use the method you want to and would have to look for an alternative like the one I suggested above.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Yes, I've tried this and you're right. That's why it gives a compiler limit error. Very frustrating. Guess I need to dynamically write the whole script tag from the server...
 
Try this:

1. Add an id to your head tag:
Code:
<head id="head1" runat="server">

2. Look at the Literal control in the head tag and replace it's Text property:
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            CType(head1.Controls(1), LiteralControl).Text = CType(head1.Controls(1), LiteralControl).Text.Replace(".js", ".js?updated=" & System.DateTime.Now.ToString("yyyy-MM-dd"))
        End If
    End Sub


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top