Search 
DailyCoding > Web

The Script tag runat="server" Problem Solution Using ResolveUrl

Solution to the script tag runat="server" problem
Author admin on Jul 21, 2008 14 Comments
Rate it    (Rated 4 by 9 people)
6,517 Views

This might be an easy trick, but I ran into this issue a while ago; we can not specify the runat="server" attribute on a JavaScript file link as we do for images and style sheet.

<link id="mainSheet" runat="server" href="~/Images/styles.css" 
rel="stylesheet" type="text/css" />

By using runat=server the href of the link will automatically be resolved by the asp.net in we have placed ~/ in the path. This gives us advantages when we are linking this into a control and the control could be used by other pages places at a different directory level.

Same problem arise with using linked JavaScript. But this cannot be solved by using runat=server tag. Here is an alternative way to overcome this problem.

<script language="javascript" src="<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>" type="text/javascript"></script>
ASP.NET | Web

Discussion

Stefan On Oct 1, 2008 05:41 AM
Where is the solution??

Daily Coder On Oct 1, 2008 11:10 PM
@Stefan, sorry there was some problem with html code. It is corrected now. Thanks for reporting :).

kkkkkkkk On Feb 25, 2009 05:01 AM

Swati On Jun 8, 2009 06:25 AM
hi m new to asp.net
wanna knw as script tag is for client side then why we use runat attribute in it???

Jason Y On Jul 6, 2009 09:18 AM
Thanks for the post; it should solve the problem I am currently working through with #include'd files (instead of user controls)--though I might just replace #include'd files with user controls instead (good long term solution).

@Swati
The script tag is no longer for client side if you use runat="server" attribute, which is the very purpose for using it, and the very reason why that will _not_ work for resolving the "~/..." path in a script tag the way it works with other tags (<link /> in the example above)--Indeed, using runat="server" will resolve the "~/..." path, but will have the additional (undesirable) effect of making the script run on the server.

Either way, separate code files ("code-behinds) are generally preferred over <script runat="server">...</script>.

prawin On Jul 23, 2009 12:57 AM
Many thanks.. This solved my problem. Actually iam using URL rewriting..which has multiples keywords in the URL separated with a "/".

Relative paths will not work if you are using url rewriting..in that case i thought "~" would solve the problem but it didn't work for script tags..

prawin On Jul 23, 2009 12:57 AM
Many thanks.. This solved my problem. Actually iam using URL rewriting..which has multiples keywords in the URL separated with a "/".

Relative paths will not work if you are using url rewriting..in that case i thought "~" would solve the problem but it didn't work for script tags..

Prashant Atal On Aug 20, 2009 01:51 AM
I tried this but it only works in the Form tag and not in the Head tag :(.

renato On Oct 12, 2009 11:59 AM
I am working on my first ASP.Net project, and this just did the trick.
Thanks a lot!

John Holliday On Jan 6, 2010 04:21 PM
Prashant,

It works in the Head tag if you use <head runat="server">.

Derek On Jan 20, 2010 12:00 PM
I also found that prefacing the src attribute with HttpRuntime.AppDomainAppVirtualPath also solves the problem.

<code>
<script type="text/javascript" src="<%= HttpRuntime.AppDomainAppVirtualPath %>"/path_to_file.js"></script>
</code>

Derek On Jan 20, 2010 12:02 PM
Oops... I closed quotes after embedded asp tag... now it's fixed.


<script type="text/javascript" src="<%= HttpRuntime.AppDomainAppVirtualPath %>/path_to_file.js"></script>

Filip Urbanowicz On Feb 19, 2010 08:42 AM
Hi,
I have the same problem.

<head runat="server" id="Head" >
<title>to set</title>
<link href="~/obout/grid/styles/style/style.css" rel="stylesheet" media="all" type="text/css" />
<script language="javascript" type="text/javascript" src="~/Common/globalMain.js" ></script>
</head>

the ~ works with css links. But not with scripts !! why ?

I'v tried <%=ResolveUrl(...) %> -> Have an server error, does'nt work with head runat=server
I've tried HttpRuntime.AppDomainAppVirtualPath --> Same Error

Derek it does'nt work
Does anybody have a solution ?

Thanks a lot



Mahesh On Mar 3, 2010 03:09 AM
Hi you can added up the scripts in scriptmanager's to avoid the ur problem

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved