6

I am trying to create a widget using javascript and jquery in SalesForce, I was stuck with a ERROR: Uncaught TypeError: Cannot call method 'noConflict' of undefined

Below is my code which i am using

(function() {

// Localize jQuery variable
var $j;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "https://ap1.salesforce.com/resource/go/jquery.min.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    $j = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    $j = window.jQuery.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    $j(document).ready(function($) { 
        /******* Load CSS *******/
       var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "style.css" 
        });
        css_link.appendTo('head');          

        /******* Load HTML *******/
        var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
        $.getJSON(jsonp_url, function(data) {
          $('#example-widget-container').html("This data comes from another server: " + data.html);
        });

    });
}

})(); // We call our anonymous function immediately

In the browser console ERROR is pointed to the first line of the scriptLoadHandler function.

Vivek P
  • 3,000
  • 6
  • 23
  • 31
  • you have not included jQuery in your page – Arun P Johny Jun 26 '13 at 09:05
  • Yes I do, I am including it dynamically by check with if its existing or not, you can see it in the 3rd line of the above code. – Vivek P Jun 26 '13 at 09:09
  • when i open https://ap1.salesforce.com/resource/go/jquery.min.js it does not seem to contain the jquery code. have you tried including jquery from somewhere else? – Reason Jun 26 '13 at 09:14
  • I have tried with this http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js also. – Vivek P Jun 26 '13 at 09:21

1 Answers1

3

Declare local jquery variable as below :

//Localize jQuery variable
var $j = jQuery.noConflict();
Tejas Savaliya
  • 318
  • 4
  • 7