Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Not really a question, but....

Not really a question, but....

Scheduled Pinned Locked Moved JavaScript
questionjavascript
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wombaticus
    wrote on last edited by
    #1

    Why does anyone use jQuery for AJAX calls? In fact, for a lot of things? OK, there's no point in reinventing the wheel, and people have built some nice things with it, notably photo sliders/ carousels etc, but for so many things - and AJAX is a case in point - good ol' vanilla JavaScript is so much easier! A number of times now I've looked at using jQuery for AJAX, and given up in frustration. No more. Sledgehammers and nuts come to mind.

    Richard DeemingR F 2 Replies Last reply
    0
    • W Wombaticus

      Why does anyone use jQuery for AJAX calls? In fact, for a lot of things? OK, there's no point in reinventing the wheel, and people have built some nice things with it, notably photo sliders/ carousels etc, but for so many things - and AJAX is a case in point - good ol' vanilla JavaScript is so much easier! A number of times now I've looked at using jQuery for AJAX, and given up in frustration. No more. Sledgehammers and nuts come to mind.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Wombaticus wrote:

      for so many things - and AJAX is a case in point - good ol' vanilla JavaScript is so much easier!

      Really?

      if (window.XMLHttpRequest) {
      httpRequest = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }

      httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState == 4 && httpRequest.status == 200) {
      var result = httpRequest.responseText;
      // Do something with the result
      }
      };

      httpRequest.open("GET", theUrl, true);
      httpRequest.send();

      vs:

      $.ajax({
      url: theUrl
      // Other options...
      }).done(function(result){
      // Do something with the result...
      });

      Also, the jQuery version[^] has a lot of built-in functionality which you would have to reimplement if you wanted to use it in the plain Javascript version.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      W 1 Reply Last reply
      0
      • W Wombaticus

        Why does anyone use jQuery for AJAX calls? In fact, for a lot of things? OK, there's no point in reinventing the wheel, and people have built some nice things with it, notably photo sliders/ carousels etc, but for so many things - and AJAX is a case in point - good ol' vanilla JavaScript is so much easier! A number of times now I've looked at using jQuery for AJAX, and given up in frustration. No more. Sledgehammers and nuts come to mind.

        F Offline
        F Offline
        F ES Sitecore
        wrote on last edited by
        #3

        One of the nice things about jQuery is that it hides per-browser implementation from the client; you don't need to know the differences between how IE\Chrome\FireFox implements ajax, or even if there are differences in how individual versions work. People also keep jQuery up-to-date again so you don't have to. Add to that the syntax is much easier as Richard has already pointed out, and it has nice features you'd have to code yourself such as making "get" calls non-cached etc. If the only reason you're using jQuery is for ajax then you might have a point, but if I'm honest even if ajax was all I was doing I'd probably still use jQuery as it is lightweight and you'll probably end up using the other features anyway.

        W 1 Reply Last reply
        0
        • F F ES Sitecore

          One of the nice things about jQuery is that it hides per-browser implementation from the client; you don't need to know the differences between how IE\Chrome\FireFox implements ajax, or even if there are differences in how individual versions work. People also keep jQuery up-to-date again so you don't have to. Add to that the syntax is much easier as Richard has already pointed out, and it has nice features you'd have to code yourself such as making "get" calls non-cached etc. If the only reason you're using jQuery is for ajax then you might have a point, but if I'm honest even if ajax was all I was doing I'd probably still use jQuery as it is lightweight and you'll probably end up using the other features anyway.

          W Offline
          W Offline
          Wombaticus
          wrote on last edited by
          #4

          Well, I don't find the syntax (or documentation) clear at all. Maybe I should turn this into a question: how would you, for example, "jQueryify" this?

          if (window.XMLHttpRequest) {
          rQM = new XMLHttpRequest();
          } else if (window.ActiveXObject) {
          rQM = new ActiveXObject("Microsoft.XMLHTTP");
          }

          function getPage(p, tImg, iImg, t, s, h) {
          var r = new Date().getTime();
          var url = 'pagebox.ashx?p=' + p.toString() + '&r=' + r.toString();
          rQM.open("GET", url, true);
          rQM.onreadystatechange = function () {
          UpdateScreen(p, tImg, iImg, t, s, h);
          }
          rQM.send(null);
          }

          function UpdateScreen(p, tImg, iImg, t, s, h) {
          if (rQM.readyState == 4) {
          var response = rQM.responseText;
          if (response != '') {
          var params = response.split("|");
          document.getElementById(tImg).value = 'userfiles/' + params[0];
          document.getElementById(iImg).src = 'userfiles/' + params[0];
          document.getElementById(t).value = params[1];
          document.getElementById(h).value = params[2];
          tinyMCE.get(s).setContent(params[3]);
          }
          }
          }

          (tinyMCE references the tinyMCE HTML editor[^]) To me, that is pretty easy to grok - I've tried over and over to work out how to do it in jQuery and, as I say given up each time in despair... (And, yes, I know it's a little ugly in the return - I should probably return the data as XML or JSON, but it works well enough for my purposes.)

          Richard DeemingR 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Wombaticus wrote:

            for so many things - and AJAX is a case in point - good ol' vanilla JavaScript is so much easier!

            Really?

            if (window.XMLHttpRequest) {
            httpRequest = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }

            httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var result = httpRequest.responseText;
            // Do something with the result
            }
            };

            httpRequest.open("GET", theUrl, true);
            httpRequest.send();

            vs:

            $.ajax({
            url: theUrl
            // Other options...
            }).done(function(result){
            // Do something with the result...
            });

            Also, the jQuery version[^] has a lot of built-in functionality which you would have to reimplement if you wanted to use it in the plain Javascript version.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            W Offline
            W Offline
            Wombaticus
            wrote on last edited by
            #5

            Well.. see my reply to F-ES Sitecore below.... (if you feel so inclined ...)

            1 Reply Last reply
            0
            • W Wombaticus

              Well, I don't find the syntax (or documentation) clear at all. Maybe I should turn this into a question: how would you, for example, "jQueryify" this?

              if (window.XMLHttpRequest) {
              rQM = new XMLHttpRequest();
              } else if (window.ActiveXObject) {
              rQM = new ActiveXObject("Microsoft.XMLHTTP");
              }

              function getPage(p, tImg, iImg, t, s, h) {
              var r = new Date().getTime();
              var url = 'pagebox.ashx?p=' + p.toString() + '&r=' + r.toString();
              rQM.open("GET", url, true);
              rQM.onreadystatechange = function () {
              UpdateScreen(p, tImg, iImg, t, s, h);
              }
              rQM.send(null);
              }

              function UpdateScreen(p, tImg, iImg, t, s, h) {
              if (rQM.readyState == 4) {
              var response = rQM.responseText;
              if (response != '') {
              var params = response.split("|");
              document.getElementById(tImg).value = 'userfiles/' + params[0];
              document.getElementById(iImg).src = 'userfiles/' + params[0];
              document.getElementById(t).value = params[1];
              document.getElementById(h).value = params[2];
              tinyMCE.get(s).setContent(params[3]);
              }
              }
              }

              (tinyMCE references the tinyMCE HTML editor[^]) To me, that is pretty easy to grok - I've tried over and over to work out how to do it in jQuery and, as I say given up each time in despair... (And, yes, I know it's a little ugly in the return - I should probably return the data as XML or JSON, but it works well enough for my purposes.)

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Something like this should work:

              function getPage(p, tImg, iImg, t, s, h) {
              $.get({
              url: 'pagebox.ashx?p=' + p.toString(),
              cache: false
              }).done(function(response) {
              updateScreen(p, tImg, iImg, t, s, h, response);
              });
              }

              function UpdateScreen(p, tImg, iImg, t, s, h, response) {
              if (response != '') {
              var params = response.split("|");
              document.getElementById(tImg).value = 'userfiles/' + params[0];
              document.getElementById(iImg).src = 'userfiles/' + params[0];
              document.getElementById(t).value = params[1];
              document.getElementById(h).value = params[2];
              tinyMCE.get(s).setContent(params[3]);
              }
              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              W 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Something like this should work:

                function getPage(p, tImg, iImg, t, s, h) {
                $.get({
                url: 'pagebox.ashx?p=' + p.toString(),
                cache: false
                }).done(function(response) {
                updateScreen(p, tImg, iImg, t, s, h, response);
                });
                }

                function UpdateScreen(p, tImg, iImg, t, s, h, response) {
                if (response != '') {
                var params = response.split("|");
                document.getElementById(tImg).value = 'userfiles/' + params[0];
                document.getElementById(iImg).src = 'userfiles/' + params[0];
                document.getElementById(t).value = params[1];
                document.getElementById(h).value = params[2];
                tinyMCE.get(s).setContent(params[3]);
                }
                }


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                W Offline
                W Offline
                Wombaticus
                wrote on last edited by
                #7

                As simple as that, huh...? sigh... :-O

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups