I have multiple posts to display in single page. My Java code generates html dynamically, and assign a number to each div.
<div onclick=\"postClicked(" + i + ");\" style=\"cursor: pointer;\">
When I click to a div, I want to go to ReadPost.xhtml page and displayed the details about the clicked post.
function postClicked(id) {
localStorage.setItem( 'objectToPass', id );
location.href = 'ReadPost.xhtml';
}
In the ReadPost page I have the following code:
<h:inputHidden id="postInput" value="#{postManager.clickedPostId};"/>
<script>
var myData = localStorage['objectToPass'];
document.getElementById("postInput").value = myData;
</script>
<h:outputText value="#{postManager.displaySinglePost()}" escape="false" />
I want to set the hidden input value with the data in the localStorage. In this way when displaySinglePost() method runs clickedPostId will be already updated and I can display the page with the correct post id. However, postManager.displaySinglePost() runs before the script. How can I make script run before postManager.displaySinglePost() method is called?