0

I am trying to get this codepen http://codepen.io/eternalminerals/pen/qdGvMo working on my wordpress page at http://eternalminerals.com/test/

I know that since Wordpress is in no-conflict mode, I have to change the $ to jQuery, but I did that and made sure the script was in the header as well (using this JS adder plugin css-javascript-toolbox) but it still isn't working like the codepen. I tested the codepen without the javascript and it behaves like the wordpress page, so I know the issue must be in the javascript.

<script type='text/javascript'>
StarWars = (function() {

  /* 
   * Constructor
   */
  function StarWars(args) {
    // Context wrapper
    this.el = jQuery(args.el);

    // Audio to play the opening crawl
    this.audio = this.el.find('audio').get(0);

    // Start the animation
    this.start = this.el.find('.start');

    // The animation wrapper
    this.animation = this.el.find('.animation');

    // Remove animation and shows the start screen
    this.reset();

    // Start the animation on click
    this.start.bind('click', jQuery.proxy(function() {
      this.start.hide();
      this.audio.play();
      this.el.append(this.animation);
    }, this));

    // Reset the animation and shows the start screen
    jQuery(this.audio).bind('ended', jQuery.proxy(function() {
      this.audio.currentTime = 0;
      this.reset();
    }, this));
  }

  /*
   * Resets the animation and shows the start screen.
   */
  StarWars.prototype.reset = function() {
    this.start.show();
    this.cloned = this.animation.clone(true);
    this.animation.remove();
    this.animation = this.cloned;
  };

  return StarWars;
})();

new StarWars({
  el : '.starwars'
});
</script>

There are no javascript console errors on my website page but it behaves as if there is no javascript controlling the html like in the codepen. Any help would be greatly appreciated. Thank you!

eternalminerals.com
  • 395
  • 1
  • 4
  • 14

2 Answers2

2

If you want to quickly update your jQuery call and pass in jQuery to it's own function then you can do so as shown below:

   jQuery(document).ready(function( $ ) {

   // $ Works! You can test it with next line if you like
   // console.log($);

   });

Additionally, and I've executed this methodology myself below, you can pass jQuery in as the dollar sign. NOTE: All jQuery code that uses "$.apis()" instead of "jQuery.apis()" must be within this code snippet which you may also load from an external script if necessary.

    (function($) {

    // $ Works! You can test it with next line if you like
    // console.log($);

    })( jQuery );

You can find the link to examples with more detail here: (https://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/)

This solution is designed for wordpress, but I was using in a CMS-serving engine that rendered all the JS files on the backend and only sent out a view. In that scenario I could never use the dollar sign declaration in a typical document.ready function as you are trying so our issue is identical, just with a different engine behind it. This should help, cheers and happy coding!

Mike Horstmann
  • 580
  • 2
  • 9
  • awesome link!! thanks for the detailed explanation!! – eternalminerals.com Aug 19 '15 at 06:17
  • This works additionally in pre-render CMS systems like LogiXML, Logi Analytics, and Logi Info. Any CMS that renders all of the content and then sends that rendered content out to the view (including the JS) utilizes this methodology. – Mike Horstmann Aug 20 '15 at 22:53
0

Ok, thanks to Wordpress embedded javascript on page doesn't work it seems like all I had to do was surround the script with a jQuery ready function:

jQuery(function() {
  /* your code here */
)};

http://eternalminerals.com/test/ works now woohoo!

Community
  • 1
  • 1
eternalminerals.com
  • 395
  • 1
  • 4
  • 14