### Example B4J Handler updating HTML Selector

B4J server application displaying a "Tip of the Day" when webpage is opened.
This is an example of updating html selectors in a html file without using websockets.
The "Tip of the Day" is send via the B4J Response Write method to the html page.
The javascript function get_tipofday() updates the html selector tipofday using Ajax.
The "Tip of the Day" is randomly taken from a textfile (located in the dirapp folder).
When the page is loaded and refresh is clicked, a new "Tip of the Day" is displayed.
HTML - Index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<h1>Tip of the Day</h1>
<hr>
<div id="tipofday"></div>
<hr>
<p>Have a great Day...</p>
<script type="text/javascript">
function get_tipofday()
{
$.ajax({
url: "/GetTipOfDay",
success: function(result)
{
$("#tipofday").html(result);
},
});
}
$(document).ready(function()
{
get_tipofday();
});
</script>
</body>
</head>
B4J - tipofdayEx.b4j
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: false
#End Region
`Sub Process_Globals
Public srvr As Server 'The webserver
End Sub`
`Sub AppStart (Args() As String)
srvr.Initialize("srvr")
srvr.Port = 51042
srvr.AddHandler("/GetTipOfDay", "GetTipOfDay", False)
srvr.Start
StartMessageLoop
End Sub`
B4J - gettipofday.bas
`Sub Class_Globals
End Sub`
`Public Sub Initialize
End Sub`
`Private Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim l As List = File.ReadList(File.DirApp, "tipofday.txt")
Dim tipofday As String = l.Get(Rnd(0, l.Size - 1))
resp.Write("

").Write(tipofday).Write("

")
End Sub`
Textfile - tipofday.txt
Tip of the Day 1
Tip of the Day 2
Tip of the Day 3
Tip of the Day 4
Tip of the Day 5

Changed: , Category: WebApps, File: T6C277M2063.htm, ID: 2063