0

SharePoint 2010 Server. I edited section of my masterpage like this(in sharepoint designer, not programmatically):

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

But how can I ensure that I don't connect jQuery twice? Something like this

    <script>if (typeof jQuery == 'undefined') { 
    // jQuery is not loaded} 
    else {
    // jQuery is loaded
    }
    </script>

but how can I dynamically load jQuery from here? Or if I edit .masterpage, I should look by myself if there is no jQuery already added?

2
  • Why can't you just open the master page and remove any duplicate references? Jan 1, 2015 at 19:33
  • ufff) I thought there is an easy way.. Thank You for response!)
    – Gennady G
    Jan 1, 2015 at 19:50

2 Answers 2

1

Here is script you can use to dynamically load jQuery if it not present.

// Only do anything if jQuery isn't defined
if (typeof jQuery == 'undefined') {
    if (typeof $ == 'function') {
        // warning, global var
        thisPageUsingOtherJSLibrary = true;
    }
    function getScript(url, success) {
        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 function provided as param
                success();                  
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);                   
            };          
        };          
        head.appendChild(script);       
    };      
    getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', function() {       
        if (typeof jQuery=='undefined') {           
            // Super failsafe - still somehow failed...         
        } else {            
            // jQuery loaded! Make sure to use .noConflict just in case
            fancyCode();                
            if (thisPageUsingOtherJSLibrary) {
                // Run your jQuery Code
            } else {
                // Use .noConflict(), then run your jQuery Code
            }           
        }       
    });     
} else { // jQuery was already loaded       
    // Run your jQuery Code
};

http://css-tricks.com/snippets/jquery/load-jquery-only-if-not-present/

1

The follwoing code will try to load JQuery from Google CDN. If it fails, then it will look for a local copy of JQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script> 
<script>
window.jQuery || document.write('<script src="/_layouts/15/JQuery/jquery-1.7.1.min.js" type="text/javascript"></script>');
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.