I want to write the result of $('body').html() in localStorage . How to make sure that the result does not contain javascript code and write down only the page elements?

    1 answer 1

     // Клонируем <body/> var $cloneBody = $('body').clone(); // Удаляем все теги script $cloneBody.find('script').remove(); // Выводим в консоль, чтоб убедиться, что нет тегов script console.log($cloneBody.html()); // Сохраняем в localStorage localStorage.setItem('key', $cloneBody.html()); 
    • But could you tell me how to correctly check if the script exists on the page. While I do something like this "If there is no script with ID, then I create a script" Everything is OK in the browser returns 304, BUT if you reload like a crazy page, errors will appear (i.e., the scripts do not have time to load) I tried to add myscript to create. async = 'false' Does not help: (And so grateful to you, but my problem is not completely solved) - Zimzibar
    • I don’t quite understand the context of the problem, because here somehow the status of the 304 response was worded, although it was about working with the DOM. Explain what script you are creating and as a result the answer appears with the status 304 - dakiesse
    • Let's go in order: -You gave me the right answer, it fits. -But as with this decision, it turned out that all the same you need to leave some scripts so that when changing the 'body' js scripts remain in the browser’s memory. - In this regard, I made a check, if there is a script (I find it by ID) then when changing the 'body' the script is not executed (the same) and the status is returned 304 (that is, returned from the browser’s cache, which saves resources) - Good. I said to myself and started testing and with repeated refresh (F5) of the page I find errors (meaning that the scripts did not load) - Zimzibar
    • What do you have when after refreshing the page? - dakiesse
    • the main index.php page returns without a working jQquery The fact is that if I check for a script, when I refresh, I get a 304 notification and there is a twofold situation and it happens if it's crazy to refresit, then the error crashes jQuery is not defined, and if I refresh a little later then everything works ... - Zimzibar