C# 如果jQuery是在纯HTML中添加的,如何检查jQuery是否是从C代码加载的?
“我正在开发一个WEB部件,我想知道是否有某种方法可以找出将要插入WEB部件的母版页项目中是否存在任何JQUERY库。 我想这样做:C# 如果jQuery是在纯HTML中添加的,如何检查jQuery是否是从C代码加载的?,c#,jquery,sharepoint,user-interface,C#,Jquery,Sharepoint,User Interface,“我正在开发一个WEB部件,我想知道是否有某种方法可以找出将要插入WEB部件的母版页项目中是否存在任何JQUERY库。 我想这样做: if (jQuery) { // jQuery is loaded Page.ClientScript.RegisterClientScriptInclude(typeof(WebpartSlideShow), "jQuery", "/_layouts/Jquery-Cycle/jquery-1.5.1.mi
if (jQuery) {
// jQuery is loaded
Page.ClientScript.RegisterClientScriptInclude(typeof(WebpartSlideShow), "jQuery", "/_layouts/Jquery-Cycle/jquery-1.5.1.min.js");
Page.ClientScript.RegisterClientScriptInclude(typeof(WebpartSlideShow), "jQueryCycle", "/_layouts/Jquery-Cycle/jquery.cycle.all.min.js");
// break;
} else {
// jQuery is not loaded
Page.ClientScript.RegisterClientScriptInclude(typeof(WebpartSlideShow), "jQueryCycle", "/_layouts/Jquery-Cycle/jquery.cycle.all.min.js");
}
但是,如果jQuery是以纯HTML格式添加的,那么它就不起作用,而只有当jQuery是通过
页面添加的。ClientScript
您无法从c#检查jQuery是否包含在目标页面中。您只能在运行时使用js从组件中检查这一点。我可以想象的唯一方法是使用jQuery设置cookie,然后在服务器端检查cookie是否存在
我不是C#开发人员,因此无法帮助理解确切的语法,但原则应该是相同的。尝试注册您自己的JavaScript代码,它应该检查jQuery是否已经加载,如果没有加载。下面是跨浏览器的示例
function initScript(url, callback) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0],
done = false;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function () {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
callback();
};
};
head.appendChild(script);
};
function initJQuery(callback) {
//if the jQuery object isn't available
if (typeof (jQuery) == 'undefined') {
initScript("/_layouts/YourWebPart/jquery-1.7.1.min.js", function() { callback(); });
} else {
callback();
}
}
initJQuery(function () {
alert('loaded');
});