views:

573

answers:

5

Hi,

We're having some trouble with including javascript in masterpages. The "~/" root shortcut doesn't seem to work, and so we're having to manually enter in the path to the javascript from where it will be used, for example: "../../tooltip.js"

However, the problem is that if the nested page is in a different path, this approach does not work as the path stays the same despite the nested page being in a different location - any solutions on how to get the path automatically working like it does for the css files?

Thanks!

A: 

Can you explain what you mean by the "~/" isn't working? It should be exactly what you're looking for. You might check that your head tag has the runat="server" attribute, since that might prevent the "~/" from working.

Hugoware
Not sure what the down vote was for since what I said is true... -- Hmmm...
Hugoware
The ~/ doesn't resolve correctly for javascript include tags. (I didn't vote down however)
stringo0
Bah! I stand corrected!! -- I knew it worked for tags inside the head tag (like for stylesheets) -- Didn't realize it didn't work for scripts!
Hugoware
+4  A: 

The ~/ is a special entity in ASP.NET which represents the "application root". The translation only happens when you pass that URL through an ASP.NET server control, such as <asp:Image runat="server" ImageUrl="~/path..." />. Trying to use it in something that is passed as literal text directly to the client, such as ` will be rendered exactly that in the browser.

There are a few solutions to this:

  1. Put your scripts in a predictable place relative to the domain root, such as domain.com/scripts/script.js, and you can refer to it as /scripts/script.js. The preceding / tells the client (browser) it is an absolute path from the domain root.

  2. Use Page.ResolveClientUrl to render the correct path (<%=Page.ResolveClientUrl("~/script./js")%>)

  3. Create your own server control which handles the ~/ resolution.

  4. Embed the script as an assembly resource.

Rex M
Thank you for the detailed solution - the absolute path method seems easy enough if it works.
stringo0
A: 

Try

<script type="text/javascript" src=<%=Request.ApplicationPath+"/Scripts/Script_Name"%>></script>

I've been using url rewriting and "~" occasionally chokes on special characters in the url even when they are encoded.

apocalypse9
+4  A: 

Cory Larson recommends:

<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/tooltip.js") %>"></script>
lance
This is the correct way, missed the tilde. This works, I'll remove mine.
ScottE
I favor a slightly different syntax:`<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/") %>tooltip.js"></script>`
Ken Browning
Thanks for the quick fix!
stringo0
A: 

If the script tag is in the HEAD element just use a path that is relative to the master page and asp.net will automatically fix the reference for you. Just make sure that the HEAD element has the runat=”server” attribute.

This also works really well for CSS files. It's the only way I've been able to get them to resolve in the VS.NET designer.

Here is a sample from my project:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="../styles/reset.css" />
    <link rel="stylesheet" type="text/css" href="../styles/960.css" />
    <link rel="stylesheet" type="text/css" href="../styles/default.css" />
    <link rel="stylesheet" type="text/css" href="../styles/buttons.css" />
    <script type="text/javascript" src="../jQuery/jquery-1.3.2.js"></script>
</head>
Scott Dowding