开发者

ASP.Net: Dynamic JavaScript path / src

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.m开发者_如何学Pythonin.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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜