我正在为SharePoint 2013进行软件开发。其中一部分涉及覆盖SharePoint的文件预览器(filepreview.debug.js变为myfilepreview.debug.js)。但是,我们遇到了IE8的问题。在IE9中一切正常。
IE8中引发的错误会导致您在网站集中访问的任何网站上出现错误,我们的自定义功能已激活:“对象不支持此属性或方法”
在对此错误进行一些研究后,出现 IE8根本不支持Object.create
。 This Mozilla Developer post似乎支持这一理论。当问题通过在抛出错误的行之前添加此polyfill代码来解决问题时,我更加迫切地相信这一点:
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() { }
F.prototype = o;
return new F();
};
}
我想这是有意义的,因为它模仿了Object.create的功能,据说IE8不支持它。
然而,这令人困惑,因为SharePoint的文件预览器javascript工作得很好。他们的javascript 也使用Object.create。甚至更奇怪,在我们的javascript中抛出错误的代码部分甚至不是我们的代码 - 它是SharePoint的。到目前为止,整个javascript代码实际上与SharePoint相同,除了几行。
这是默认值,直到有问题的行:
function $_global_filepreview() {
RegisterSod("mediaplayer.js", "_layouts/15/mediaplayer.js");
RegisterSod("sp.publishing.resources.resx", "/_layouts/15/ScriptResx.ashx?name=sp.publishing.resources&culture=" + STSHtmlEncode(Strings.STS.L_CurrentUICulture_Name));
RegisterSodDep("mediaplayer.js", "sp.publishing.resources.resx");
previewBase = (function() {
ULS7RK:
;
var filePreviewUniqueId = 0;
return {
init: function(ctxT, listItem, extension) {
this.fpId = ++filePreviewUniqueId;
this.fpDomId = "FilePreviewID-" + String(this.fpId);
this.fpCtx = ctxT;
this.fpExtension = extension;
this.fpListItem = listItem;
},
getHtml: function() {
ULS7RK:
;
return ['<div class="js-filePreview-containingElement" id=', StAttrQuote(this.fpDomId), '>', this.getInnerHtml(), '</div>'].join("");
},
getDomId: function() {
ULS7RK:
;
return this.fpDomId;
},
getContainingElement: function() {
ULS7RK:
;
var containingElement = document.getElementById(this.fpDomId);
Sys.Debug.assert(m$.isElement(containingElement), "Containing element has not been rendered yet.");
return containingElement;
},
canRender: function() {
ULS7RK:
;
return true;
},
getLoadingIndicatorHtml: function(customStyle) {
if (m$.isUndefined(customStyle)) {
customStyle = "";
}
return ['<div class="js-filePreview-loading-image" style="width:', this.getWidth(), 'px; height:', this.getHeight(), 'px; line-height:', this.getHeight(), 'px; text-align:center; vertical-align:middle; display: inline-block; ' + customStyle + '">', '<img src="', "/_layouts/15/images/gears_anv4.gif", '" />', '</div>'].join("");
},
hideLoadingIndicator: function() {
ULS7RK:
;
var containingElement = document.getElementById(this.fpDomId);
((m$(containingElement)).find("div.js-filePreview-loading-image")).css({
display: "none"
});
},
getInnerHtml: function() {
ULS7RK:
;
return "";
},
getWidth: function() {
ULS7RK:
;
return null;
},
getHeight: function() {
ULS7RK:
;
return null;
},
onPostRender: function() {
},
onVisible: function() {
},
onHidden: function() {
}
};
})();
//**This is where the "fix" was added originally**
blankPreview = Object.create(previewBase); <--- error is thrown here
SharePoint javscript(上图)与我们之间(截至目前)之间的唯一区别是我们从一开始就删除了以下两行,但添加它们仍然无法解决问题:
RegisterSod("sp.publishing.resources.resx", "/_layouts/15/ScriptResx.ashx?name=sp.publishing.resources&culture=" + STSHtmlEncode(Strings.STS.L_CurrentUICulture_Name));
RegisterSodDep("mediaplayer.js", "sp.publishing.resources.resx");
所以我的问题是:为什么我收到这个错误,IE8中不支持Object.create,而默认javascript中其他相同功能的使用没有问题?
编辑:另一个论坛上的用户建议我尝试通过草皮注册我的脚本。我将此添加到我的代码中没有效果:
RegisterSod("MyFilepreview.debug.js", "_layouts/15/MyFilepreview.debug.js");
答案 0 :(得分:3)
尝试在IE中进行调试,并将断点放在SharePoint的默认JavaScript文件中。您确定默认JavaScript中的Object.create实例是否真的被命中了?
答案 1 :(得分:3)
他们没有使用 Object.create
正确的参数。
他们使用 Object.create({name:value})
时应为Object.create({name:{value:value}})
。
所以他们可能定义了他们自己的Object.create
并且他们的代码在你的代码之后被使用,所以你的代码运行时你已经设置了Object.create
,并且他们可能像你一样测试存在并假设它们是他们的实际上它是你的版本。
因此,请在代码中检查Object.create
的定义,并检查脚本的执行顺序。