开发者

jQuery Script src declaration throws JavaScript error despite NoConflict being called

开发者 https://www.devze.com 2023-04-06 03:32 出处:网络
I\'m using a survey designing program that gives (some) freedom when programming your own custom functions/design/etc. By default, it uses Prototype. I want to use jQuery as well. In the header I decl

I'm using a survey designing program that gives (some) freedom when programming your own custom functions/design/etc. By default, it uses Prototype. I want to use jQuery as well. In the header I declare jQuery and call noConflict, like so:

<script src="SurveyResource/jquery.js"> </script>
<script>
jQuery.noConflict();
</script>

When I try to load the page, I get the classic "Object doesn't support this property or method" error. I also tried removing the noConflict script, and the error still occurs with just the declaration. After a quick debug, I found that the error occurs in prototype.js, at this function:

fire: function(element, eventName, memo) {
  element = $(element);
  if (element == document && document.createEvent && !element.dispatchEvent)
    element = document.documentElement;

  var event;
  if (document.createEvent) {
    event = document.createEvent("HTMLEvents");
    event.initEvent("dataavailable", true, true);
  } else {
    event = document.createEventObject();
    event.eventType = "ondataavailable";
  }

  event.eventName = eventName;
  event.memo = memo || { };

  if (document.createEvent) {
    element.dispatchEvent(event);
  } else {
    element.fireEvent(event.eventType, event);
  }

  return Event.extend(event);
}

Specifically, at this line: element.fireEvent(event.eventType, event);. Also, I'm using IE8. Sorry about that. Can anybody help me to get these two kids to play nice together? The related questions don't seem to have any answers.

EDIT: here's a fiddle of what I think is a reproduction of my problem: http://jsfiddle.net/vbzju/7/

Prototype is selected as the framework (that's what the survey software uses), but then I add jQuery as a header script, call noConflict, and then try a simple jQ开发者_运维技巧uery function. I think the error thrown is the same as the error I'm getting on my machine.


In your fiddle, jquery was being loaded twice. Here is an example of using a closure so that you can use the $ sign for both jquery and prototype. http://jsfiddle.net/vbzju/14/ Also, you should load jquery first and then prototype.


I found also that NoConflict is not enough to make Internet Explorer look less buggy. The following modification to prototype.js function fire (around line 5631) worked for me: since everything works fine with Firefox, Safari and Chrome, I check the version token in the navigator.userAgent string and avoid calling dispatchEvent if I find MSIE. I added the str and n local variables. n is null for browsers other than MSIE:

function fire(element, eventName, memo, bubble) {
element = $(element);

if (Object.isUndefined(bubble))
  bubble = true;

if (element == document && document.createEvent && !element.dispatchEvent)
  element = document.documentElement;

var event;
if (document.createEvent) {
  event = document.createEvent('HTMLEvents');
  event.initEvent('dataavailable', true, true);
} else {
  event = document.createEventObject();
  event.eventType = bubble ? 'ondataavailable' : 'onfilterchange';
}

event.eventName = eventName;
event.memo = memo || { };

var str=navigator.userAgent; 
var n=str.match(/MSIE/g);

if ((document.createEvent) && (n == null))
  element.dispatchEvent(event);
else
  element.fireEvent(event.eventType, event);

return Event.extend(event);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消