2

I have same problem as "element.dispatchEvent is not a function" js error caught in firebug of FF3.0 but in google chrome.

I have wrote <script>jQuery.noConflict();</script> after all scripts. But now I have another problem:

  ...
  $("a.try").click(function () {
  ...

undefined is not a function

and

 ...
 var blockHeight = $(window).height();
 ...

undefined is not a function

Thus first fix is not valid.

P.S.

html part where I include scripts:

....
<script type="text/javascript"
        src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript"
        src="<c:url value='/resources/js/underscore.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/jquery-1.8.2.min.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/jquery.mousewheel.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/popup.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/jquery.jscrollpane.min.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/scroll-startstop.events.jquery.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/jquery-ui-1.10.0.custom.min.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/script-ini.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/map/map.js'/>"></script>
<script type="text/javascript"
        src="//maps.google.com/maps/api/js?sensor=true&js?v=3.exp&libraries=places"></script>

<script type="text/template" id="terminal-template">
    ...
</script>
<style>
    .grey-terminal {
        background-color: rgb(226, 226, 226);
    }
</style>
<script type="text/javascript"
        src="<c:url value='/resources/js/addTerminal.js'/>"></script>
<script>jQuery.noConflict();</script>
...

Can you advise another way?

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

2

When you call jQuery.noConflict(); you no longer associate jQuery with the $ variable.

So things like $().click or $(selector) will throw errors. Instead, you can do something like this:

jQuery.noConflict();
jQuery('a.try').click(handler);
jQuery(window).height();

Or, you can also assign jQuery to a new variable, like this:

var j$ = jQuery.noConflict();
j$('a.try').click(handler);
j$(window).height();

http://jsfiddle.net/wL4mc03a/

Edit

As for dispatching events, one way you can do this (using jQuery) is to trigger the event. Say you have some code that looks like this:

jQuery(document).on('keydown', function(e){
    if (e.which === 42) {
        alert('That is the key to life!');
    }
});

Later, you can trigger this event. Instead of triggering the event using a string ('keydown'), we'll create an jQuery event and pass that.

var evt = jQuery.Event('keydown');
evt.which = 42;

jQuery(document).trigger(evt);
Jack
  • 9,151
  • 2
  • 32
  • 44
  • Is it possible to rewrite var 'event = new KeyboardEvent('keydown'); $("#pac-input").dispatchEvent(event);' without jquery? – gstackoverflow Nov 22 '14 at 18:38
  • 1
    Yes, you can. However, you'll need to call it like `$('#pac-input').get(0).dispatchEvent(event)`. `get` will return the DOM node (rather than the jQuery-wrapped object). However, if you want to do it the jQuery way (which I recommend), you could just `trigger` the event like `$('#pac-input').trigger('keydown')` (if you need to specify the key, you can pass a second argument as an object like `$('#input').trigger('keydown', {which:42})` - the second object simulates the event object. – Jack Nov 23 '14 at 18:55
  • **var event = new KeyboardEvent('keydown',{which:42}); $("#pac-input").get(0).dispatchEvent(event);** When I invoke this I see that executes event which invoke when I type text. i need invoke enter event – gstackoverflow Nov 23 '14 at 20:09
  • **($('#pac-input').trigger('keydown', {which:42})** doesn't work – gstackoverflow Nov 23 '14 at 20:14
  • Oh, for enter, you'll need to pass the correct `keycode` (which isn't 42, I believe). How does your code check to see if it's an enter key? Are you using `e.which`? I'll throw together a fiddle for you really quickly... – Jack Nov 23 '14 at 21:23
  • Ok, here's the fiddle: http://jsfiddle.net/46e7tksr/ - I apologize as I gave incorrect info earlier. The additional arguments from trigger are not passed in the event object, but rather, as an additional argument (to the event) - so you would need to check for (e.which || data.which) - if you use trigger. – Jack Nov 23 '14 at 21:34
  • Here's using jQuery's version of dispatchEvent (and creating a new event) - http://jsfiddle.net/46e7tksr/1/ Which removes the need to check a second argument (since you're creating an event) - This, for me, is cleaner – Jack Nov 23 '14 at 21:34
1

This helped me! Adding jQuery.noConflict(); just before starting the jquery code and then replacing all '$' with 'jQuery'.

<script type="text/javascript">
  
    jQuery.noConflict();
    jQuery(document).ready(function() {
        ....
        ....
    });
</script>

Same as mentioned in the above solution @Jack