如果在纯HTML中添加jQuery,如何检查是否从C#代码加载jQuery?

时间:2012-07-31 14:43:35

标签: c# jquery sharepoint user-interface

正在开发一个WEB_PART,我想知道是否有某种方法可以找出是否在主页面项目中存在任何JQUERY LIB,其中将插入Web部件。 我想做这样的事情:

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格式添加的,但只有在通过Page.ClientScript

添加jQuery时才会起作用

3 个答案:

答案 0 :(得分:0)

您无法检查c#中的jquery是否包含在目标页面中。您只能在运行时使用js。

从组件中检查

答案 1 :(得分:0)

你可以想象的唯一方法就是用jQuery设置一个cookie,然后以某种方式检查服务器端是否存在这个cookie。

我不是C#开发人员,所以无法帮助确切的语法,但原则应该是相同的。

答案 2 :(得分:0)

尝试注册您自己的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');    
});