I was recently trying to find a bug in some scripting and I discovered this very interesting behavior when loading a page with jQuery.
File #1: Test1.htm
<div id="test"></div>
<script type="text/javascript">
$(document).ready(function(){
$('#test').load('test2.htm #content',function(){
alert('done loading!');
})
})
</script>
File #2: Test2.htm
<div id="content">
howdy
<script type="text/javascript">
$(document).ready(function(){
alert('hello #1');
})
</script>
<SCRIPT type="text/javascript">
$(document).ready(function(){
alert('hello #2');
})
</SCRIPT>
</div>
Now when I run Test1.htm, I get the following:
- hello #2 alert
- howdy from test2.htm displays
- done loading alert
As you can see the only difference is the script tag is in upper case for the hello #2 alert. The script to show the hello #1 alert never gets exectued...
So far, I've tested this in Firefox 3.55, IE 8.0.6001.18702 and Chrome 3.0.195.33 with similar results.
In the past, I've wanted to execture javascript from the loaded content, similar to this SO question. So my question is, is this a bug or a solution?
Update: As Jitter states below, the same thing happens if Test2.htm has the script outside the content I am loading.
<div id="content">
howdy from test2.htm
</div>
<script type="text/javascript">
$(document).ready(function(){
alert('hello #1');
})
</script>
<SCRIPT type="text/javascript">
$(document).ready(function(){
alert('hello #2');
})
</SCRIPT>