We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • discuss.answer
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    AJAX with JQuery is really easy. You have an HTML element in your page where you want custom data; say it has an id attribute like id="block1". You have a resource with no template that just has the data you want; this can be like any resource content with snippets, chunks, etc. to generate the block of content. If you entered the URL to this resource in your browser, you'd see the actual block of content you want and nothing else.

    Then your JQuery would look like this:
    $('#block1').load('block1.html');

    As you can see, it simply says to load the block1.html content into your block1 div.

    You would have one of these for each of the if conditions as mentioned in your earlier post.
    $(window).load(function() {
        var windowSize = $(window).width();
          if (windowSize <= 767) {
                $('#block1').load('block1_phone.html');
            }
            else if (windowSize <= 979) {
               $('#block1').load('block1_pad.html');      
            }
            else if (windowSize >= 980) {
               $('#block1').load('block1_full.html');     
            }
    });




      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 8168
      • 1,118 Posts
      Quote from: sottwell at Apr 29, 2013, 10:53 AM
      AJAX with JQuery is really easy. You have an HTML element in your page where you want custom data; say it has an id attribute like id="block1". You have a resource with no template that just has the data you want; this can be like any resource content with snippets, chunks, etc. to generate the block of content. If you entered the URL to this resource in your browser, you'd see the actual block of content you want and nothing else.

      Then your JQuery would look like this:
      $('#block1').load('block1.html');

      As you can see, it simply says to load the block1.html content into your block1 div.

      You would have one of these for each of the if conditions as mentioned in your earlier post.
      $(window).load(function() {
          var windowSize = $(window).width();
            if (windowSize <= 767) {
                  $('#block1').load('block1_phone.html');
              }
              else if (windowSize <= 979) {
                 $('#block1').load('block1_pad.html');      
              }
              else if (windowSize >= 980) {
                 $('#block1').load('block1_full.html');     
              }
      });





      Sounds awesome Susan! I will have a look into that...
        • 38290
        • 712 Posts
        The width of a users viewport is not a constant, even on mobile
          jpdevries
          • 8168
          • 1,118 Posts
          Quote from: sottwell at Apr 29, 2013, 10:53 AM
          AJAX with JQuery is really easy. You have an HTML element in your page where you want custom data; say it has an id attribute like id="block1". You have a resource with no template that just has the data you want; this can be like any resource content with snippets, chunks, etc. to generate the block of content. If you entered the URL to this resource in your browser, you'd see the actual block of content you want and nothing else.

          This approach is brilliant - how have I never known about this before! Works a dream... Thanks so much Susan - this solution will sort things here perfectly.
            • 21671
            • 244 Posts
            Why you don't use mediaqueries to check for screen width and then apply classes like .showMobile or hideMobile to make sections of page visible or hidden? You can also pull in modernzr to use mediaqueries or yepnope I think, to test for screen width and then pull in certain javascripts depending on the results of the test. You can see this strategy in action at http://www.kindflow.com. view source in firefox and you'll see how I accomplished it. You will also see that I used this technique to either show or not show a random background image. I also routinely use this tactic to have two nav schems in a page and then only display one of them depending on the width of the browsers. I've found this to be a much cleaner and easier development process.

            -noah
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              If totally different functions are to be run on the server for the different sizes, then loading it all and showing and hiding stuff in the CSS will complicate the processing and horribly bloat the download for all of the clients. Sometimes you really do need to serve different content to the different devices.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 8168
                • 1,118 Posts
                Quote from: noahlearner at Apr 29, 2013, 09:20 PM
                Why you don't use mediaqueries to check for screen width and then apply classes like .showMobile or hideMobile to make sections of page visible or hidden? You can also pull in modernzr to use mediaqueries or yepnope I think, to test for screen width and then pull in certain javascripts depending on the results of the test. You can see this strategy in action at http://www.kindflow.com. view source in firefox and you'll see how I accomplished it. You will also see that I used this technique to either show or not show a random background image. I also routinely use this tactic to have two nav schems in a page and then only display one of them depending on the width of the browsers. I've found this to be a much cleaner and easier development process.

                -noah

                Thanks Noah - I will have a look at all of that // I appreciate your thoughts
                  • 38290
                  • 712 Posts
                    jpdevries