Skip to main content

The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.


IntentionJS and RequireJS - How monkeys do it (the sfw edit)


  • RequireJS (and some knowledge of how to use it)
  • IntentionJS
  • A brain
  • Bananas

There are plenty of reasons you shouldn’t HAVE to use IntentionJS, but it’s just so good when you NEED your fix, of DOM manipulation.

Normally a responsive website should be designed so that when you expand or collapse the viewport (effective screen), the DOM elements flow naturally from left to right, and top to bottom. The order is preserved, and it was designed so that those elements follow that flow in terms of importance and usability.

Admittedly, this does limit us at times, and we need elements to be in completely different placements in the DOM, depending on the device used. Sure we can mess with duplicated content by hiding and showing the right elements based on the screen size [please don’t, it’s bad], or doing some fancy schmancy css floating and absolute positioning, but then you start to get other fun issues, that go beyond the scope of this banana log.

So, we’re left with manually moving the elements around. You could start using the append() and after() functions say, from jQuery, but that also gets complicated.. setting screen widths, window resize, or using (media query), etc. All messy in some form or another.

We like clean, even though we like poo, we still like clean.

Our Hero, IntentionJS

From the mouths of the intentionJS magicians: (

“Intention.js offers a light-weight and clear way to dynamically restructure HTML in a responsive manner.”

Good right? riiight.

Okay, so we all have our own methods for including JS, and writing libraries and code. Here’s what we monkeys do.

Starting with RequireJS

We like using requireJS. A lot. In fact I get a banana every time I do. So in the main.js file loaded by require we have:

  1. requirejs.config({
  2. paths: {
  3. // vendor
  4. 'intention': 'vendor/intention',
  5. 'underscore': 'vendor/underscore-min',
  6. 'viewportsize': 'vendor/viewportSize-min',
  7. // custom
  8. 'jquery': 'modules/jquery-global',
  9. 'intentcontext': 'modules/intentcontext',
  10. 'homepage': 'modules/homepage',
  11. 'initintent': 'modules/initintent'
  12. },
  13. });

Here we are just telling require where to find all our ‘required’ files when we ‘require’ them. Yeah.. there is probably a better way of saying that. *puts on deal with it glasses*

Intention requires underscore, so we’re including that. Also, we’re using a little library called ‘viewportsize’. Why? well because different browsers report different viewport sizes, based on whether or not it’s showing the scroll bar. That’s a problem, this fixes that problem. (

Then we include jQuery, cause we need it. Then comes some magical code with unicorns.. and monkeys.

  1. require([
  2. 'jquery',
  3. 'underscore',
  4. 'intentcontext',
  5. ], function ($, _, IntentContext) {
  6. 'use strict';
  7. // DOM ready
  8. $(function() {
  9. // js loaded only on homepage
  10. if ($('body').hasClass('front')) {
  11. require(['homepage']);
  12. }
  13. if ($('html').length > 0) {
  14. require(['initintent']);
  15. }
  16. }); // DOM ready
  17. });

So here, we’re just including the needed libraries for the site in general, and then checking if we’re on the homepage, if so, include the homepage module. Then the very last that we include in the initialization of the intent. Think of it, as intentions’s big red “go” button. We’ll get to these a bit later. For now, just know, that we are making sure that the initinent file is included last, since we’re doing all ‘intention’ setup first. Since require loads these in ORDER, of the code inclusion, we’re able to do all the setup first, then lastly initialize it.


This is where we setup our ‘contexts’. A context is basically a ‘switch point’ - a point a which stuff is supposed to happen. Each ‘context’ is associated to a screen size (these values should match your CSS media queries for major layout changes)..

  1. IntentContext.bp_desktop = 1025;
  2. IntentContext.bp_tabletlandscape = 769;
  3. IntentContext.bp_tablet = 641;
  4. IntentContext.bp_mobilelandscape = 321;
  5. IntentContext.bp_mobile = 0;

These are the breakpoints were major layout changes happen (for the purpose of this blog). Yours would match that of your CSS breakpoint values.

Next up, making our contexts. As you will see, each context has a name, and I’m setting the “min” value to the breakpoint value that I set in the above code. So basically, the ‘desktop’ context will get triggered every time the browser hits the “1025 pixel” viewport width or above. (It won’t keep re-triggering events though, as you increase viewport width above that, which is nice.) All the other ‘contexts’ will get triggered at their respective screen width values.

  1. IntentContext.horizontal_axis = IntentContext.intent.responsive({
  2. ID: 'width',
  3. contexts: [{
  4. name: 'desktop',
  5. min: IntentContext.bp_desktop
  6. }, {
  7. name: 'tabletlandscape',
  8. min: IntentContext.bp_tabletlandscape
  9. }, {
  10. name: 'tablet',
  11. min: IntentContext.bp_tablet
  12. }, {
  13. name: 'mobilelandscape',
  14. min: IntentContext.bp_mobilelandscape
  15. }, {
  16. name: 'mobile',
  17. min: IntentContext.bp_mobile
  18. }],
  19. matcher: function (measure, context) {
  20. return measure >= context.min;
  21. },
  22. measure: function () {
  23. IntentContext.v_width = viewportSize.getWidth();
  24. return IntentContext.v_width;
  25. }
  26. });

So, there is a thing. It’s thing you may need. Normally intention won’t activate the context on first page load, which you may need. We will get to that. (This is what that initintent.js file is for).


Now we need to tell which elements where to be placed in the dom, according to whatever ‘context’ is triggered. You can either go directly in the HTML and add all the special intention attributes to the elements, or do it via JS. I like doing it in the JS, i find it cleaner.

So in our Homepage.prototype.intent = function () function:

  1. var footer = $('.l-footer');
  2. footer.attr('intent', '');
  3. footer.attr('in-desktop-after', '.l-header');
  4. footer.attr('in-tabletlandscape-after', '.l-main');
  5. footer.attr('in-tablet-after', '.l-main');
  6. footer.attr('in-mobilelandscape-after', '.l-header');
  7. footer.attr('in-mobile-after', '.l-header');
  8. IntentContext.intent.on('desktop', function() {
  9. footer.attr('style', 'border: 4px solid red;');
  10. });
  11. IntentContext.intent.on('tabletlandscape', function() {
  12. });
  13. IntentContext.intent.on('tablet', function() {
  14. });
  15. IntentContext.intent.on('mobilelandscape', function() {
  16. footer.attr('style', 'border: 4px solid white;');
  17. });
  18. IntentContext.intent.on('mobile', function() {
  19. footer.attr('style', 'border: 4px solid blue;');
  20. });

First line we just get the element we want to target. The next lines are key.

I’m now manually adding all the required contexts on that elements, so for each ‘breakpoint’ context, we know where to place the footer. The syntax is as follows

  1. footer.attr(‘in-[your-breakpoint-name]-[move-function],[dom element])

‘your-breakpoint-name’ is just the name you associated to the breakpoint, up in the IntentContext.js file.

‘move-function’ is the method in which you want to place that element. They work just like jQuery’s manipulation functions [append(), before(), after(), prepend()]

‘dom-element’ is just the element you are specifying to be “moved to”.

So in this case, when the browser hits the ‘desktop’ layout screen width, we are putting the ‘.l-footer’ element just after the ‘.l-header’ element in the DOM. The next lines all work the same and specify where the element needs to go, for whichever context (screen size).

Then, we have some more magical code.

  1. IntentContext.intent.on('desktop', function() {
  2. footer.attr('style', 'border: 4px solid red;');
  3. });
  4. IntentContext.intent.on('tabletlandscape', function() {
  5. footer.attr('style', '');
  6. });

So, for each context, we can run some custom code of any kind. In this case, every time we hit the ‘desktop’ context, we are going to add a border to the footer element. Everytime we hit the ‘tabletlandscape’ context, we make sure remove any lingering styles. Etc..

I normally like to use these methods to ‘reset’ certain things that may have been triggered on an alternate layout.

Lastly, the Initilization of Intent. This will allow us to use those .on() [in above code] functions on page load as well.

Know that all this will only happen on the homepage though. If you need this to happen on all pages, you can create a separate module that can handle site wide context changes, and just include it in the main.js require section.


  1. IntentContext.horizontal_axis.respond();
  2. IntentContext.intent.elements(document);
  3. $(window).on('resize', IntentContext.horizontal_axis.respond);

So these 3 lines just get everything going. Check out the intention.js website for further detail, but suffice to know, they get intention up and running.

That should be it to have it all working nicely and being able to do some sexy DOM manipulation without to much pain.

Web Development

Would you like to know more about what we do?

View Our Services

Graphic Design Cheeky Monkey

Have a look at some our client work

View Our Work

Cheeky Monkey Discovery

Learn more about Cheeky Monkey Media

About Us