4

I am loading content using ajax from external HTML Files. Dont know why after loading content, click event is not working in safari (mobile safari as well) on any of the newly loaded elements (ul, li, images etc). However this is working in mozilla.

I am not able to understand the exact issue behind this. Please advice me the solution. Below is the code for reference.

Note: I am using the below code under jquery ready function. Is jquery is the cause of issue??

var currentBottle = this.title; var request = createRequest(); if (request == null) { alert("Unable to create request"); return; } request.onreadystatechange = showContent; request.open("GET", currentBottle + ".html", true); request.send(null);

function showContent() { if (request.readyState == 4) { if (request.status == 200) { document.getElementById("food_scroller").innerHTML = request.responseText; } } }

user504023
  • 553
  • 1
  • 7
  • 12
  • Is this a duplicate? http://stackoverflow.com/questions/16598213/how-to-bind-events-on-ajax-loaded-content – Nikkorian Apr 11 '17 at 14:38

3 Answers3

11

Event binding breaks down when the content of a page loaded through ajax. You have to bind the events again in the document using the below procedure.

do this

$(document).on('click', '.classname' ,function (e) {
    // write your logic
});

instead of

$('.classname').on('click' ,function (e) {
    // write your logic
});
Umar Raza
  • 211
  • 1
  • 6
  • 19
3

In Safari the content-type can sometimes matter, make sure the response type is set as text/html. Also in your AJAX loaded content you should try not to have <script> tags I don't think Safari respects those sometimes.

Maybe try to use jQuery's $.load() to GET HTML content cross-browser compatible (below is equivalent to your createRequest and showContent functions):

var currentBottle = this.title;
$.load(currentBottle + ".html",
    function(responseText,textStatus,XMLHttpRequest){
        $("#food_scroller").html(responseText);
        //bind you on click events here
        $("#food_scroller").find("ul, li, images").click(myClickFunction);
  }
);
Steven
  • 575
  • 7
  • 13
  • 1
    +1 Your loaded content should not have ` – timdream Nov 27 '10 at 18:52
  • Thanks timdream I was not confident, but fairly certain. I will remember the ` – Steven Nov 27 '10 at 18:55
  • Thanks for the providing the suggestion, however i am not including any script with loaded content. To make every thing more clear. – user504023 Nov 27 '10 at 19:56
  • in mobile safari, jquery evernts works very slow i cant use them. And i am not including any script with loaded content. I am just loading unordered list from external page. To make every thing more clear. I am calling above mention code in click event. Actually as per the functionality, i have images thumbnails at the top and below to this there is another set of images(a long row-scrollable), when ever the user click on above thumbnail images new set of images should appear at the bottom replacing the previous images. – user504023 Nov 27 '10 at 20:09
  • Till here, every thing is fine, but click event is not working on these newly loaded images. Is there any other alternative apart from live event. – user504023 Nov 27 '10 at 20:10
  • Also i dont want to use jquery events, because of the performance issues in mobile safari. – user504023 Nov 27 '10 at 20:13
  • Mobile Safari... ok, I am learning a lot today. So are your events attached to the tag via `` attribute? Try looking into `window.addEventListener` and `object.attachEvent` – Steven Nov 27 '10 at 20:38
  • "As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()." – Nikkorian Apr 11 '17 at 14:33
0

I have been through this issue myself. Newly loaded html via Ajax needs to be connected to the event handlers, even if it is replacing existing DOM elements. To do that you have to include in your AJAX result to establish a handler for all event-sensitive elements that are being rendered.

So for example, if your AJAX call returns

$ajaxresult = '<button class="bookbtn" id="bookbtn" type="button" >Book</button>';

You need to add

$ajaxresult .= '<script>$("#bookbtn").on("click",function(){doit($(this))});</script>'."\n";

However in researching this issue just now, I read the following in http://api.jquery.com/on/ :

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to .on(). To ensure the elements are present and can be selected, place scripts after the elements in the HTML markup or perform event binding inside a document ready handler. Alternatively, use delegated events to attach event handlers.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements: ...

Nikkorian
  • 770
  • 4
  • 10