Сообщение от рони
			 
		
	 | 
	
		так вы же сами назначили его после загрузки html строка 4.$(function(){ 
 
а строка 9 (напрямую/сразу) 
строка 9 сработает раньше чем строка 5
	 | 
	
Подскажите пожалуйста как мне переделать этот кусок кода под динамическое добавление на страницу, чтобы он работал корректно, так же как в статическом его варианте ? 
<html><head><title>second page</title></head><body><p>test script</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
    alert("Hello 1");
    $(function(){
        var text2 = "test message here\n\n11\n2\n3";
        TEST = text2, window.onbeforeunload = function(a) {
            var c = TEST;
            return a.returnValue = c;
        }, window.onload = function() {
            if (confirm(TEST)) {
                alert("its okay");
            } else {
                alert("cancelled");
            }
        };
    });
    window.addEventListener("load", function (){
        alert ("onload 222");
    });
    $(document).ready(function(){
        alert(jQuery.fn.jquery);
    });
</script>
<script>alert("Hello 2")</script>
<div>the 7 tag</div><div>the 8 tag</div><div>the 9 tag</div></body></html>