Running scripts in an ajax-loaded page fragment

前端 未结 4 1811
谎友^
谎友^ 2021-01-23 06:40

My web app dynamically loads sections of its UI with jquery.ajax. The new UI sections come with script though. I\'m loading them as such:

Use...



        
4条回答
  •  庸人自扰
    2021-01-23 07:12

    The problem is that the scripts included in the dynamic part of the page run before the new page fragment is inserted into the DOM. But often these scripts want to modify the HTML they're being delivered with.

    I'm fairly sure that in that case, the only sensible thing is to place the script after the HTML element.

    Everything else would become kludgy quickly - I guess you could implement your own "ready" handler that gets executed after your HTML has been inserted, but that would be a lot of work to implement for no real gain.

提交回复
热议问题