I'm curious about the effects of the following scenarios:
Suppose I'm defining a jQuery click event, for example
$(function()
{
$('.someElement').click(function() { /*do something*/ };
};
which gets loaded in some arbitrary HTTP GET
request.
But this .someElement
does not initially exists in the DOM when requesting.
Now if I also have an asynchronous AJAX call which inserts some piece of html
that does have this .someElement
defined, for example
$.ajax(
{
//options omitted for readability
success: function(responseData)
{
$('#elementToInsert').html(responseData);
}
});
loads and inserts <div class="someElement"></div>
into the page.
It seems that when clicking on the .someElement
element, the script in the jQuery click event gets executed.
It somehow bugs me that this works. Would someone explain me why this works?
Also imagine the next scenario:
As in the previous example, I'm requesting the same arbitrary HTTP GET
. But in this request, the click event is not included. The same ajax
call is included though.
Now the html in the responseData
looks like this
<div class="someElement"></div>
<script>
$(function()
{
$('.someElement').click(function() { /*do something*/ };
};
</script>
When I click on .someElement
, I get the exact same behavior as in the first scenario. This bugs me even more, so could someone also explain to me why this works?
Some notes:
- The
responseData
that gets inserted into the DOM, would be somewhere before the jQuery library. - I don't think it should matter which server side framework is used, but in my case it would be ASP.NET MVC 5.
- These scenarios are tested in the most recent versions of the following browsers: IE, Firefox, Chrome.
To summarize my question: How is it possible that the mentioned scenarios works (e.g. that the jQuery gets executed)?
P.S. The funny thing is that in the answer for this question, it's explained that
JavaScript inserted as DOM text will not execute.
while in my case it is executed.
It also refers to another question that explains that eval()
has to be called in order to execute javascript.
Also this article explains the opposite of my scenarios and mentions the use of eval()
.