jeudi 16 avril 2015

Javascript Function Not Being Called after dynamic loading

I have a weird issue that i am hoping someone can help resolve.


Problem


When i load html dynamically via .load() function, if any aspect of html in the loaded fragment tries to access the javascript query functions in original HTML page, it doesn't work. Example code below:


Main HTML page (main.html)



<!DOCTYPE html>
<html xmlns:th="http://ift.tt/wfNV60">
<head lang="en">
<!--javascript load functions etc... standard header stuff -->
</head>
<body>
<div id="dynamic_section_fragment"></div>
<a href="javascript:" onclick="loadFragment()">Load Fragment</a>

<script type="text/javascript">


// <![CDATA[
function loadFragment() {
$("#dynamic_section_fragment").load("/api/fragment/");
};

$(".checkvalue").click(function () {
$.getJSON("/api/checkvalue", {term: $(this).attr('value')}, function () {
console.info("submitted for checking");
})
});
// ]]>
</script>

</body>
</html>


FRAGMENT File (fragment.html)



<!DOCTYPE html>
<html xmlns:th="http://ift.tt/wfNV60">
<head lang="en">

</head>
<body>
<div th:fragment="check_value">
<br/>
Check the value in the attribute field
<br/>
<a href="javascript:" th:attr="value='123'" class="checkvalue">Check This<a/>
</div>
</body>
</html>


SPRING MVC Controller Method



@RequestMapping("/api/checkvalue")
public String getFragment(Model model) {
return "fragment :: check_value";
}


So a run down of actions:


-Main.html page loads


-User clicks on Load Fragment hyperlink


-Javascript dynamically loads the relevant fragment into the div


-User clicks on Check This hyperlink, nothing happens


Is there something i am missing or something i need to be aware?


It is as if Thymeleaf has preregistered all the possible scenarios of events and doesn't allow any others. Only way i have been able to get it to work is by injecting the "checkvalue" javascript within the fragment, which as you can agree is a bad way of doing things.


Help is appreciated.


Aucun commentaire:

Enregistrer un commentaire