Javascript 小部件无法在 Sharepoint Web 部件内运行
我有一个 JavaScript 小部件,可以从 3D 派对网站加载内容,我需要在 sharepoint Web 部件中使用它。问题是该小部件可以在任何地方(wordpress、其他网站等)工作,但不能在共享点 Web 部件内工作。
粘贴到 Web 部件中,保存后仅显示 $0 等字符,奇怪的是,如果我粘贴像 twitter 这样的不同小部件来检索最后一条推文,它会起作用。
有谁可以看一下代码并告诉我是否有问题吗?我是 sharepoint 的新手,我必须清除网站名称和 url,因为该项目尚未公开...谢谢!
<link rel='stylesheet' type='text/css' href='xxx/stylesheets/widget.css'/>
<div id='xxx'></div>
<script type='text/javascript'>
var require = document.createElement('script');
require.src= 'xxx/javascripts/require-jquery.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(require);
var xxx = document.createElement('script');
xxx.src= 'xxx/plugins/4f357c414918c20001000003.js';
require.onload = function () {
$r = jQuery.noConflict();
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(xxx);
}
</script>
i've got a javascript widget that loads contents from a 3d party website and i need to use it in a sharepoint web part. The problem is that the widget works everywhere (wordpress, other websites etc.) but not inside a sharepoint web part.
Pasted in a web part it only shows chars like $0 after saving, the strange thing is that if i paste a different widget like the twitter one to retrive last tweets it works.
Is there anybody who could have a look at the code and tell me if there is something wrong with it? I'm a newbie with sharepoint, i had to clear the website name and url because the project is not yet bublic... thanks!
<link rel='stylesheet' type='text/css' href='xxx/stylesheets/widget.css'/>
<div id='xxx'></div>
<script type='text/javascript'>
var require = document.createElement('script');
require.src= 'xxx/javascripts/require-jquery.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(require);
var xxx = document.createElement('script');
xxx.src= 'xxx/plugins/4f357c414918c20001000003.js';
require.onload = function () {
$r = jQuery.noConflict();
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(xxx);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
"将 JavaScript 插入内容编辑器 Web 部件 (CEWP)”应该可以帮助您。
"Insert JavaScript into a Content Editor Web Part (CEWP)" should help you.
我的建议是包含 Web 部件作为嵌入资源所依赖的所有 Web 部件元素或包含在您的 _layouts 文件夹中。这将确保所有图像、xml、图标和脚本都在正确的位置运行。这里我假设 require-jquery.js 没有通过 SharePoint 解决方案 (wsp) 与您的 Web 部件一起部署。此外,您还应该在 Web 部件的 PreRender 方法中注册您的客户端脚本,或者使用以下示例作为内联代码,
希望这会有所帮助。
My suggestion would be to include all webpart elements on which a webpart depends on as an embedded resource or in you _layouts folder. This will ensure any images, xml, icons and scripts are running in a proper location. Here I am assuming require-jquery.js is not deployed with your webpart via SharePoint solution (wsp). Also you should register your client side script in webpart's PreRender method or use following example for inline code,
Hope this helps.