syed mohsin
syed mohsin

Reputation: 2938

dynamic reference to javascript file

I have a master page with a reference to javascript file that is js folder. Both File and masterpage are in the root directory.

Many of my forms are in different folders Like

  Root>first>abc.aspx
  Root>second>def.aspx
  Root>second>anotherFolder>def.aspx

I kept the reference like this

      <script type="text/javascript" src="js/Check.js"></script>

I also tried src="<%= Page.ResolveUrl("~/js/Check.js") %>" and src="~/js/Check.js". While using Page.ResolveUrl and Server.ResolveUrl page shows error.

What i need to do so that any form in any directory get the reference of the file. I dont want to do ../ and ../../ in every form.

Upvotes: 0

Views: 559

Answers (2)

Darren Wainwright
Darren Wainwright

Reputation: 30757

If you're running your Visual Studio's internal server for debugging then firstly remove the virtual folder it insists on using.

Do this by selecting the root of your site in VS Solution explorer, then right-click and choose "Properties Window" - in the properties change "Virtual Path" from your "AppName" to /

This virtual path plays havoc with all sorts of paths..I find that if i don't make this change in VS then when I DO get all my paths working they won't work when I put the site on a live server that isn't using a virtual site.

Then set your JS reference to <script type="text/javascript" src="/js/Check.js"></script> - using the root / in your src.

UPDATE

If you really want to keep the virtual folder then you can try this - using ResolveClientUrl():

First, add Runat="Server" to your <head>

<head runat="server" id="myhead">

<script type="text/javascript" src='<%=ResolveClientUrl("~/js/check.js") %>' />

Upvotes: 1

&#214;mer Faruk Aplak
&#214;mer Faruk Aplak

Reputation: 911

<script type="text/javascript" src="/js/Check.js"></script>

you can use "/" --> web site root

Upvotes: 4

Related Questions