Sunday, 15 September 2013

Nest an AJAX function in a Javascript Function

Nest an AJAX function in a Javascript Function

I have AJAX code
<!-- Body Code -->
Listeners: <span id="cc_stream_info_listeners"></span>
<!-- Footer Code -->
<script type="text/javascript"
src="http://mysite.com/system/streaminfo.js"></script>
<script type="text/javascript"
src="http://mysite.com/js.php/radio/streaminfo/rnd0"></script>
Which returns dynamic listener count for an online radio stream. This code
works fine, but the js files are on a remote host. So I don't really know
how they work.
I want the page title of one of my pages to dynamically include the
listener count. Much like a twitter page counts the new tweets that are
available in your feed.
E.g. (5) My Dashboard | My Site Name Where the number is the dynamic
listener count from the AJAX function.
Here is what I have so far.
<!-- START: PAGE TITLE LISTENER CONTENT -->
<script>
function updatetitle()
{
var txt1 = "(";
var txt2 = "5";
var txt3 = ") My Dashboard | My Site Name";
var n = txt1.concat(txt2,txt3);
document.title = n;
}
</script>
<script>
window.onload = function()
{
updatetitle();
};
</script>
Simply replacing the text "5" with <span id="cc_stream_info_listeners">
doesn't work. But at least I know I have the page update component
correct. As it's working as expected with the plain text.
How can I merge these two functions - So that listener count is updated
dynamically?

No comments:

Post a Comment