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

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>

21 comment(S)


Stefan on Oct 1, 2008 05:41 AM

Where is the solution??

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">.

sponsored

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

Chris on Aug 17, 2011 08:30 PM

Hello maybe you can assist me with this.

I have an .aspx page that currently uses script to create a slider bar. The value of the slider bar is captured in the input element when the slider is changed. I'm trying to use server side scripts to pull the value from the input element "hvalue5." So, in turn I set the runat attribute to "server". Unfortunately, when this happens the javascript doesn't read hvalue5 as a defined element.

This client script works if the "runat=server" attribute is removed. Is there a better way to approach this?
<div class="slider" id="slider-5" tabindex="1" style= "width: 100%">
<input class="slider-input" id="slider-input-5" name="slider-input-5"/>
</div>
<input id="hvalue5" onchange="q.setValue(parseInt(this.value))" value="50" runat="server"/>
<script type="text/javascript">
var q = new Slider(document.getElementById("slider-5"),
document.getElementById("slider-input-5"));

q.onchange = function () {
document.getElementById("hvalue5").value = q.getValue();
};
q.setValue(50);
q.getMinimum(0);
q.getMaximum(100);
window.onresize = function () { q.recalculate(); }
</script>

Chris on Oct 10, 2011 02:11 PM

Filip - you should try '<%#ResolveUrl(...) %>' instead of <%=ResolveUrl(...) %>

Robert on Nov 29, 2011 12:25 PM

You may get an error using the 'ResolveUrl' approach : "The controls collection cannot be modified because the control contains code blocks. This post got me beyond this issue:

http://leedumond.com....ontains-code-blocks/

Dave on May 3, 2012 12:39 PM

If you are a programmer who has done backpostURLs, understands SQL hosting, and data transfer, with some HTML background. I am looking for Los Angeles Based people to run a few sites. Basic Java scripting with class and method creation. A few tricks and a Zumma Zu are great if you have that talent. No web templet crap. or E store developers please. I need raw computer programmers who can do code and understand the orders of operations. Now you don't have to do it all and I can work with Humans but I am getting tired of doing it all as a top level coder. Perl or Linux is a bonus with some PHP or Python. I need this crap running and can't find anyone to do the work. Trust is a big issue as well. This is critical data and dont want it screwed up. Now I can walk you through it but you dont get paid till I get paid. Thats how this business works. So please e mail me @ christianjesus@att.net
And if you are an asshole who likes to torture animals or some sick freak , just don't even apply. I need people with some morals and a God of mercy not the one with horns.

bvbv on Jan 10, 2013 05:16 AM

bvbv

Samrat Pyaraka on Feb 24, 2014 03:25 AM

man that stuff was pretty cool...it worked n thank you for it

Christophe on Feb 26, 2014 03:09 PM

Hi,
Thanks for the solution ! I was stuck on my javascript problem, and the src="<%=ResolveUrl("~/Scripts/bootstrap.min.js")%>" solved my problem fine.

Thanks again.

thumbs up

Leave a comment