2017-04-12 49 views
-1

我想要做一個新的Ajax請求後停止執行一些JavaScript代碼後裝入新的之後,我想加載新的代碼。殺老JavaScript和Ajax請求

這裏是我做過什麼至今:

$.ajax({ 
    url: '../../files/php/user_actions/user_checkout.php' + '?rel=tab', 
    success: function(data) { 
     $("#content").html(
      $('<div />').html(data).find("#content").html() 
     ); 

     //execute JavaScript 
     var a = document.getElementsByTagName('script'); //Replace "document" with the received data?! 
     for (var n = 0; n <= a.length; n++) { 
      var source = $(a[n]).attr('src'); 
      if (source !== undefined) { //only use the scripts containing a src-attr 
       console.log(source); 
       $(a[n]).remove(); //remove objects from the DOM 
       $.getScript(source); //execute the scripts 
      } 
     } 
    } 
}); 
+5

你究竟是什麼意思?*「停止執行一些代碼」*或*「殺死舊javascript」*?問題不明確 – charlietfl

+1

我從頁面重定向用戶後,我想爲不同的頁面使用不同的JavaScript代碼。 因爲如果用戶返回到上一頁並按下按鈕,那麼每次JavaScript代碼被導入並再次執行時,頁面將會使http請求翻倍。 – Hexdec

+0

請[編輯問題](http://stackoverflow.com/posts/43375647/edit)用,如果你想幫助過''