Buying Convert jquery to pure javascript

Discussion in 'Programming' started by Zoti Media Group, May 20, 2014.

  1. #1
    Hello programmers. I have some lines code that should be converted into pure javascript code.

    Here is the code that should be converted:

    var site_url='http://mysite.com/';$(document).ready(function(){GenerateAds();});function GenerateAds()
    {$response=[];$(".buysiteads-ad").each(function(index){$widget=$(this);$id=$widget.data('id');$type=$widget.data('type');$size=$widget.data('size');$widget.html('Loading...');$widget.css('position','relative');$params='id='+$id+'&type='+$type+'&size='+$size;send($widget,$params,$type);});}
    function send($widget,$params,$type)
    {$.getJSON(site_url+'api/'+$type+'/?callback=?',$params,function(data){jsonParse($widget,data.html,$type);});}
    function jsonParse($widget,html,$type){$widget.html(html);}
    Code (markup):

    Please message me if you can do it.
     
    Zoti Media Group, May 20, 2014 IP
  2. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Just out of curiosity, why do you not want to use jQuery?
     
    HowDoYou, May 20, 2014 IP
  3. Zoti Media Group

    Zoti Media Group Notable Member

    Messages:
    1,599
    Likes Received:
    113
    Best Answers:
    2
    Trophy Points:
    265
    Digital Goods:
    2
    As Seller:
    100% - 11
    As Buyer:
    100% - 4
    #3
    my opinion is that if i use pure javascript the load time of the ads will be faster.
     
    Zoti Media Group, May 20, 2014 IP
  4. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Try cutting out some of the other functions.

    $(document).ready(function () {
        var site_url = 'http://mysite.com/';
        $response = [];
        $(".buysiteads-ad").each(function (index) {
            $widget = $(this);
            $id = $widget.data('id');
            $type = $widget.data('type');
            $size = $widget.data('size');
            $widget.html('Loading...');
            $widget.css('position', 'relative');
            $params = 'id=' + $id + '&type=' + $type + '&size=' + $size;
            $.getJSON(site_url + 'api/' + $type + '/?callback=?', $params, function (data) {
                $widget.html(data.html);
            });
        });
    });
    Code (JavaScript):
    changed from:
    var site_url = 'http://mysite.com/';
    $(document).ready(function () {
        GenerateAds();
    });
    
    function GenerateAds() {
        $response = [];
        $(".buysiteads-ad").each(function (index) {
            $widget = $(this);
            $id = $widget.data('id');
            $type = $widget.data('type');
            $size = $widget.data('size');
            $widget.html('Loading...');
            $widget.css('position', 'relative');
            $params = 'id=' + $id + '&type=' + $type + '&size=' + $size;
            send($widget, $params, $type);
        });
    }
    
    function send($widget, $params, $type) {
        $.getJSON(site_url + 'api/' + $type + '/?callback=?', $params, function (data) {
            jsonParse($widget, data.html, $type);
        });
    }
    
    function jsonParse($widget, html, $type) {
        $widget.html(html);
    }
    Code (JavaScript):
     
    HowDoYou, May 20, 2014 IP
  5. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #5
    @tejli007 you're misunderstanding the difference between a framework and a library. A framework (e.g. AngularJS) is the one that gives you other "methods" to achieve the same result which you could achieve with pure JS , while a library like jQuery gives you the ability to achieve the same result via JS's methods but with less code. A framework takes time to initialize all its components.

    The difference we are talking about here in your example, would have a visible impact when you do like five hundred widgets per instance. This is not the case, and the difference is around few miliseconds which isn't visible for the user because of the browser page rendering process.
     
    edduvs, May 23, 2014 IP