0

I load my content using ajax, says it's content.html. So in content.html there's a script tag. I found a problem with this approach. I do a console.log('debug') within content.html, and load it with ajax, and it trigger every time. How to do a flag to prevent that?

Note that I can't load the js of content.html in global scope dude to some plugin conflicts.

Thian Kian Phin
  • 921
  • 3
  • 13
  • 25
  • Could set flag variable and check that before executing the code. if it's all events...can use off() before calling on(). How you manage it partly depends on that your code does – charlietfl Jun 28 '16 at 21:32
  • Is `content.html` calling `content.html` ? – Louys Patrice Bessette Jun 28 '16 at 21:35
  • Check here.... Maybe it's your situation : http://stackoverflow.com/questions/38062450/passing-variable-from-javascript-to-php-with-ajax-post-method/38062622#38062622 – Louys Patrice Bessette Jun 28 '16 at 21:37
  • @LouysPatriceBessette No. Assume layout.html has the ajax call. – Thian Kian Phin Jun 28 '16 at 21:40
  • @charlietfl on() and off() to bind something? imagine I have a script tag within my content.html, it got loaded after the ajax of layout.html is done. – Thian Kian Phin Jun 28 '16 at 21:41
  • No .... off() then on() so if you do load script more than once you remove previous listeners before adding new ones. `$(selector).off('click').on('click', function...` – charlietfl Jun 28 '16 at 21:42
  • Can also only target the new html by looking inside parent container only. Again.. what you do depends a bit on what code is used for – charlietfl Jun 28 '16 at 21:45

2 Answers2

0

It's only an idea... since we didn't see anycode yet...
And I'm still not sure what the problem really is.

If you want to leave this script in content.hmtl but disable it when called by layout.html.

You could do something like this in layout.html:

$("script").each(function(){
    if($(this).attr("src")=="[particular source]"){
        $(this).remove();
    }
});

Or use an id if there is no src attribute.
This has to run after you got the ajax result....

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
0

I faced with the same issue. Chrome load the js many times with unique VM files and execute all of them when the html in ajax is loaded.

Hieu Nguyen
  • 263
  • 2
  • 10