I don't necessarily need to run it at server, however, I would like to use the ~/js/somefile.js
syntax.
我不需要在服务器上运行它,但是,我想使用~/js/somefile。js的语法。
Previously, I had just set everything with Absolute paths and set my project to be at the root level. SO, I'd just declare all my stylesheets, background images and javascript files something like /css/somefile.css
之前,我只是用绝对路径设置了所有内容,并将项目设置为根级。所以,我只需要声明我所有的样式表、背景图片和javascript文件,比如/css/somefile.css
However, for this project, it doesn't run as root.
但是,对于这个项目,它不是作为根用户运行的。
I can't put runat="server"
on a script tag.
我不能把runat="server"放在script标签上。
I can put it on a link tag, though.
我可以把它放到一个链接标签上。
This must be a common problem with a few simple answers.
这一定是一个常见的问题,有几个简单的答案。
5 个解决方案
#1
19
What I've always done is use a normal script
tag and put the src
in <% %>
tags, as illustrated here:
我所做的就是使用一个普通的脚本标签,将src放在<% >标签中,如下所示:
<script language="javascript" src='<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>' type='text/javascript'></script>
#2
11
You can use the ScriptManager for this:
您可以使用ScriptManager:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/js/somefile.js" />
</Scripts>
</asp:ScriptManager>
#3
3
You can use functions inside the path string, though, e.g.
不过,可以在路径字符串中使用函数。
<script type="text/javascript"
src="<%=Url.Content("~/Scripts/jquery-1.4.2.min.js") %>"></script>
However that's the ASP.NET MVC syntax for local paths - I can't remember the forms version off the top of my head.
然而这是ASP。本地路径的MVC语法——我记不起表单版本了。
#4
2
You can get fully what you want by wrapping script tag with asp:ContentPlaceHolder and the you can access it from code behind, for example set will it be executed or not by setting visible property to true or false. Here is the example:
通过使用asp:ContentPlaceHolder来包装脚本标记,您可以完全得到您想要的内容,并且您可以从后面的代码访问它,例如,通过将可见属性设置为真或假来执行它或不执行它。下面是例子:
<asp:ContentPlaceHolder runat="server" ID="PrintPreviewBlock" Visible="false">
<script id="PrintPageCall" type="text/javascript" >
$(function() {
window.print();
});
</script>
</asp:ContentPlaceHolder>
and from code behind:
从背后的代码:
PrintPreviewBlock.Visible = true;
#5
1
Taken from dailycoding.com:
从dailycoding.com
<script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script>
#1
19
What I've always done is use a normal script
tag and put the src
in <% %>
tags, as illustrated here:
我所做的就是使用一个普通的脚本标签,将src放在<% >标签中,如下所示:
<script language="javascript" src='<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>' type='text/javascript'></script>
#2
11
You can use the ScriptManager for this:
您可以使用ScriptManager:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/js/somefile.js" />
</Scripts>
</asp:ScriptManager>
#3
3
You can use functions inside the path string, though, e.g.
不过,可以在路径字符串中使用函数。
<script type="text/javascript"
src="<%=Url.Content("~/Scripts/jquery-1.4.2.min.js") %>"></script>
However that's the ASP.NET MVC syntax for local paths - I can't remember the forms version off the top of my head.
然而这是ASP。本地路径的MVC语法——我记不起表单版本了。
#4
2
You can get fully what you want by wrapping script tag with asp:ContentPlaceHolder and the you can access it from code behind, for example set will it be executed or not by setting visible property to true or false. Here is the example:
通过使用asp:ContentPlaceHolder来包装脚本标记,您可以完全得到您想要的内容,并且您可以从后面的代码访问它,例如,通过将可见属性设置为真或假来执行它或不执行它。下面是例子:
<asp:ContentPlaceHolder runat="server" ID="PrintPreviewBlock" Visible="false">
<script id="PrintPageCall" type="text/javascript" >
$(function() {
window.print();
});
</script>
</asp:ContentPlaceHolder>
and from code behind:
从背后的代码:
PrintPreviewBlock.Visible = true;
#5
1
Taken from dailycoding.com:
从dailycoding.com
<script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script>