Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
Load ajax after page load

This is the first time that I'm using ajax with jquery and I'm new on jquery I have a structure

$(document).ready(function(){
  data_url = $('.lazy_content').attr("data-url");
    data_id = $('.lazy_content').attr("data-target-id");

    $.ajax({
        url: data_url,
        type: "POST",
        beforeSend: function() {
            $(".loaderDiv").show();
            $("#" + data_id).html("");
        },
        success: function(data) {
            $(data).each(function(index, el) {
                $(".loaderDiv").hide();
                $("#" + data_id).html(data);
            });
        }
    });
});
<div class="lazy_content" data-url="/ajax/yorumlar/@Model.OtelBilgileri.seflink" data-target-id="ajax-content-1">
  <h4 class="tur-main-baslik">COMMENTS</h4>
  <div id="ajax-content-1"></div>
</div>

<div class="lazy_content" data-url="/ajax/trustyou/@Model.OtelBilgileri.seflink" data-target-id="ajax-content-2">
  <h4 class="tur-main-baslik section-head">POSTS</h4>
  <div id="ajax-content-2"></div>
</div>

as you see I have data-url this data url has my ajax-file and I'm getting my ajax file but after page loading nothing work..whats wrong with my code ?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Depending on where the javascript is in the HTML the DOM may not have loaded at the point you run the script.

encapsulating your javascript within the jquery 'on DOM loaded' function ( $(document).ready( function() { ) will fix that problem, code as follows.

$(document).ready( function() {
  $('.lazy_content').on("load", function() {
    data_url = $(this).attr("data-url");
    data_id = $(this).attr("data-target-id");

    $.ajax({
      url: data_url,
      type: "POST",
      beforeSend: function() {
        $(".loaderDiv").show();
        $("#" + data_id).html("");
      },
      success: function(data) {
        $(data).each(function(index, el) {
          $(".loaderDiv").hide();
          $("#" + data_id).html(data);
        });
      }
    })
  });
});
about 4 years ago · Santiago Trujillo Report

0

Put your code inside $( document ).ready()

and I think you need to change your code like :

$(document).ready(function() {
    data_url = $('.lazy_content').attr("data-url");
    data_id = $('.lazy_content').attr("data-target-id");

    $.ajax({
        url: data_url,
        type: "POST",
        beforeSend: function() {
            $(".loaderDiv").show();
            $("#" + data_id).html("");
        },
        success: function(data) {
            $(data).each(function(index, el) {
                $(".loaderDiv").hide();
                $("#" + data_id).html(data);
            });
        }
    })
})

Or iterate with each class i.e .lazy_content

$( document ).ready(function() {
    $('.lazy_content').each(function(){
        data_url = $(this).attr("data-url");
        data_id = $(this).attr("data-target-id");

        $.ajax({
            url: data_url,
            type: "POST",
            beforeSend: function() {
                $(".loaderDiv").show();
                $("#" + data_id).html("");
            },
            success: function(data) {
                $(data).each(function(index, el) {
                    $(".loaderDiv").hide();
                    $("#" + data_id).html(data);
                });
            }
        })
    })
})
about 4 years ago · Santiago Trujillo Report

0

Load evnet is Only usable in window,img element.

If You use Jquery load function, You can use callback function.

$("#target").load("append.html",function(){
     // callback
});

another way, you append scripts below target html.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!