使用ASP.NET将脚本注册到页面标题中? [英] Registering scripts into the page header with ASP.NET ?

查看:59
本文介绍了使用ASP.NET将脚本注册到页面标题中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义ServerControl中使用jquery-1.4.3.min.js,已将文件插入到项目解决方案的JavaScriptFiles文件夹中,因此我们可以在以下路径中找到jquery-1.4.3.min.js:
我的项目解决方案>> JavaScriptFiles>> jquery-1.4.3.min.js

然后,我将Script标记添加到Page头中,并带有以下代码:

I''m using jquery-1.4.3.min.js in my custom ServerControl, I''ve inserted the file into the JavaScriptFiles folder in my project solution, thus we can find jquery-1.4.3.min.js in the below path :
My Project Solution >> JavaScriptFiles >> jquery-1.4.3.min.js

Then I''ve added a Script tag into the Page header with the following code :

HtmlGenericControl jqueryInclude = new HtmlGenericControl("script");
jqueryInclude.Attributes.Add("type", "text/javascript");
jqueryInclude.Attributes.Add("src", "http://<path to jQuery>");
Page.Header.Controls.Add(jqueryInclude);



我想知道如何在上面的代码中编写path to jQuery.
你能指导我吗?
谢谢.



I wanna know how I should write path to jQuery in above code.
Would you please guide me ?
Thanks.

推荐答案

最简单的是相对路径.在下面的代码中,脚本位于相同的路径中:
Easiest would be a relative path. In the code below the script is located in the same path:
HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src", "/jquery-1.4.3.min.js");
this.Page.Header.Controls.Add(include); 



如果它位于子目录中,则可以执行以下操作:"/scripts/jquery-1.4.3.min.js"

您也可以通过直接在浏览器中输入URL进行测试,如果正确,则将看到脚本以文本形式返回,或者您可以选择下载该脚本.

祝您好运!



If it is located in a sub directory you could do something like: "/scripts/jquery-1.4.3.min.js"

You could also test it by entering your url directly in your browser and if that is correct, you will see the script returned as text or you get the option to download it.

Good luck!


您可以尝试使用HttpRequest对象形成完整的URL.

You could try forming full URL using HttpRequest object.

Uri server = HttpContext.Current.Request.Url;
string uriBase = string.Format("{0}://{1}:{2}", server.Scheme, server.Host, server.Port);



在这里 uriBase 为您提供了网站的url库,该库将JavaScript文件的位置与其连接在一起.

注意:
请检查IE中的源(HTML)是否多次引用JavaScript.



Here uriBase gives you the url base for your website concatenate the JavaScript file location along with it.

Note:
Please check the source (HTML) in the IE whether you are referencing the JavaScript more than once.


hello,

尝试包括ResolveUrl方法.参见下文.

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type","text/javascript");
include.Attributes.Add("src",ResolveUrl(〜/")+"JavaScriptFiles/jquery-1.4.3.min.js");
this.Page.Header.Controls.Add(include);
hello,

Try include the ResolveUrl method. See below.

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src", ResolveUrl("~/") + "JavaScriptFiles/jquery-1.4.3.min.js");
this.Page.Header.Controls.Add(include);


这篇关于使用ASP.NET将脚本注册到页面标题中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆