Sharepoint Web 部件中的外部 Javascript 文件
我正在创建一个共享点 Web 部件,我想在其中调用外部 javascript 文件。我在以下位置创建了 .js 文件
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\CustomJScripts
调用函数时,其给出的函数未找到错误。 javascript文件的位置是否错误? 以下是代码:
protected override void CreateChildControls()
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
this.ID,
"_spOriginalFormAction = document.forms[0].action;",
true);
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsClientScriptIncludeRegistered("OnMouseOverScript"))
cs.RegisterClientScriptInclude(
this.GetType(),
"OnMouseOverScript",
ResolveUrl("/_layouts/CustomJScripts/MyJS.js"));
}
private void GetData(string strSchCode)
{
Table t = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Attributes.Add("onmouseover", "return ShowInfo('AA');");
tr.Controls.Add(tc);
t.Controls.Add(tr);
this.Controls.Add(t);
}
I am creating a sharepoint web part in which i want to call external javascript file. I have created .js file in following location
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\CustomJScripts
Its giving function not found error when function is called. Is the location of javascript file wrong?
Following is the code :
protected override void CreateChildControls()
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
this.ID,
"_spOriginalFormAction = document.forms[0].action;",
true);
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsClientScriptIncludeRegistered("OnMouseOverScript"))
cs.RegisterClientScriptInclude(
this.GetType(),
"OnMouseOverScript",
ResolveUrl("/_layouts/CustomJScripts/MyJS.js"));
}
private void GetData(string strSchCode)
{
Table t = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Attributes.Add("onmouseover", "return ShowInfo('AA');");
tr.Controls.Add(tc);
t.Controls.Add(tr);
this.Controls.Add(t);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将此 javascript 添加到您的 Web 部件。在我的网络部件中,我使用这种方法:
You have to add this javascript to your webpart. In my webpart I'm using this method:
也许单引号有问题?例如使用双引号而不是单引号:
Maybe there's an issue with the single quotes? e.g. use double quotes instead of single:
我会使用 ScriptLink.Register 方法,然后将文件移至 14 \模板\布局\1033\CustomJScripts。
ScriptLink 封装了 ClientScriptManager 调用以及附加功能。 name 参数是相对路径,这就是为什么 javascript 文件需要位于 14\TEMPLATE\LAYOUTS\ LCID 目录中(其中 LCID 是您的语言编号)。
你的代码看起来像这样:
I would use the ScriptLink.Register method and then move your file to 14\TEMPLATE\LAYOUTS\1033\CustomJScripts.
ScriptLink encapsulates the ClientScriptManager calls along with additional functionality. The name parameter is a relative path, which is why the javascript file needs to be in the 14\TEMPLATE\LAYOUTS\ LCID directory (where LCID is your language number).
Your code would look something like this: