Why doesn't jQuery $ .get () embed script elements in the DOM?

I use $.get()to extract the HTML fragment from the remote server and paste this fragment into the DOM. The snippet contains several basic HTML elements and a script element. All base elements are inserted, but the script element does not work.

Fragment example:

<p>This is a paragraph</p>
<script type="text/javascript">
// JavaScript that will work with the HTML in the snippet
</script>

I checked that the script element is in the extracted snippet, looking at the resources received in the Chrome web inspector. So why $.get()not insert a script element and is it possible to do what I want to do?

Edit:

To clarify, I extracted the HTML fragment with $.get()and pasted it into the DOM with $().html(data).

+3
source share
2 answers

script , - script . html() html, innerHTML. script , ( IE ).

innerHTML HTML 5:

. script, innerHTML, , .

script, eval() script DOM.

+1

Script , . script .

- , html script . : `

_insertScripts: function(html) {
        var sfrag = '<script[^>]*>([\\S\\s]*?)<\/script>';
        var matchAll = new RegExp(sfrag, 'img');
        var matchOne = new RegExp(sfrag, 'im');
        if(html.match(matchAll)) {
            $.map(html.match(matchAll), function(script) {
                eval( (script.match(matchOne) || ['', ''])[1] );
            });
        }
    }

`

, live() jQuery, , .

+1

Source: https://habr.com/ru/post/1764173/


All Articles