最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ASP.Net: Dynamic JavaScript pathsrc - Stack Overflow

programmeradmin2浏览0评论

I try to make Folder for things in my ASP.Net Application (e.g. all with Finance in /Finance).

No I bind an JavaScript in the MainPage:

<script type="text/javascript" src="Helper/jquery-1.3.2.min.js"></script>

But when I now open ~/Finance/Payment.aspx I get an JavaScript Error with "Path ~/Finance/Helper/jquery..." not found.

What to do?

I try to make Folder for things in my ASP.Net Application (e.g. all with Finance in /Finance).

No I bind an JavaScript in the MainPage:

<script type="text/javascript" src="Helper/jquery-1.3.2.min.js"></script>

But when I now open ~/Finance/Payment.aspx I get an JavaScript Error with "Path ~/Finance/Helper/jquery..." not found.

What to do?

Share Improve this question asked Nov 27, 2009 at 15:01 PassionateDeveloperPassionateDeveloper 15.2k35 gold badges111 silver badges190 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Your path Helper/jquery-1.3.2.min.js is a relative path. So when you go into /Finance the browser is looking for jQuery in /Finance/Helper/jquery-1.3.2.min.js.

A simple way around this is to use absolute paths

<script type="text/javascript" src="/Helper/jquery-1.3.2.min.js"></script>

Or you can use a ScriptManager which allows you to use the tilde

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/Helper/jquery-1.3.2.min.js" />
    </Scripts>
</asp:ScriptManager>

As a last resort if you have issues with the ScriptManager you can also do this

<script type="text/javascript" 
        src="<%= Page.ResolveClientUrl("~/Helper/jquery-1.3.2.min.js") %>">
</script>

You could always use ResolveClientUrl in the script src attribute (you'll need to make the path to your JavaScript file an app root relative path with the "~/"):

<script type="text/javascript" src="<%= ResolveClientUrl("~/Helper/jquery-1.3.2.min.js") %>"></script>
发布评论

评论列表(0)

  1. 暂无评论