Using JQuery .ajax in Embedded HTML section

Asked by Stephen Gower

Hi, I'm trying to display the current University of Oxford term date using JOSM data from an API provided at http://api.m.ox.ac.uk/dates/today but the display only outputs "Foo" from the HTML section (detailed below) rather than the dynamic content (EG "3rd Week, Michaelmas Term"). I understood jquery is included in the embedded HTML scripts automatically. What am I missing?

The Embedded HTML region has the following (without the repeated hyphens marking start and end of copy and paste!) in Embed HTML

---------------------------------------------------------------
<p style="text-align: center"><span style="font-family:arial,helvetica,sans-serif;color:#ffcccc;font-size:30px;"><span id="oxdate">foo</span></span></p>
---------------------------------------------------------------

And this in Embed Script

---------------------------------------------------------------
<script type="text/javascript">
function EmbedInit()
{
 // Init will be called when this page is loaded in the client.
 oxdate();
}

function oxdate() {
$.ajax({
  url: 'http://api.m.ox.ac.uk/dates/today',
  dataType: 'json',
  success: function(data) {
      $("#oxdate").html(data.components.week+"<sup>"+data.components.ordinal+"</sup> Week, "+data.components.term_long+" Term");
  },
});

}

</script>
---------------------------------------------------------------

Question information

Language:
English Edit question
Status:
Solved
For:
Xibo Edit question
Assignee:
No assignee Edit question
Solved by:
Alex Harrington
Solved:
Last query:
Last reply:
Revision history for this message
Dan Garner (dangarner) said :
#1

Not easy to say without having it in a debug environment. The best way forward would be to enable the mouse from Xibo client options and then when the embedded content appears right click and View Source.

You can then save the file and debug it in its entirety.

Revision history for this message
Stephen Gower (socks-launchpad-net) said :
#2

It's something to do with Internet Explorer's settings when using an HTML file from the local filesystem.

The right-click view-source file runs fine if I save it to a web server and point IE at the file over http (or if I use Firefox or Chrome on the file saved locally) but if I point IE at that local file it just doesn't run the $.ajax function. Beyond that, I'm stumped.

Revision history for this message
Stephen Gower (socks-launchpad-net) said :
#3

I have modified code to cope with IE. It still doesn't work in xibo, but if I right-click, view-source, save-as, the resulting html file loads in IE and works fine. Is it possible to debug within the xibo client itself?

s

Embedded HTML:
------------------------
<p style="text-align: center;margin-top: 0.1em; ">
<span style="font-family:arial,helvetica,sans-serif;color:#ffffff;font-size:16px;">
<span id="oxterm">foo</span>
</span>
</p>
------------------------

Embedded Script:

------------------------
<script type="text/javascript">
function EmbedInit()
{
 // Init will be called when this page is loaded in the client.
document.getElementById('oxterm').innerHTML="wibble";
localgetJSON('http://api.m.ox.ac.uk/dates/today', function(data) {
  document.getElementById('oxterm').innerHTML=data.components.week+"<sup>"+data.components.ordinal+"</sup> Week, "+data.components.term_long+" Term";
});

}

var localgetJSON = function(url, successHandler, errorHandler) {
var xhr = new XMLHttpRequest();
  xhr.open('get', url, true);
  xhr.onreadystatechange = function() {
    var status;
    var data;
    if (xhr.readyState == 4) { // `DONE`
      status = xhr.status;
      if (status == 200) {
        data = JSON.parse(xhr.responseText);
        successHandler && successHandler(data);
      } else {
        errorHandler && errorHandler(status);
      }
    }
  };
  xhr.send();
};

</script>
------------------------

Revision history for this message
Stephen Gower (socks-launchpad-net) said :
#4

I'm still trying to get to the bottom of this. How is Internet Explorer called when it's embedded in Xibo? There must be some difference in the environment between this and loading IE from the start menu and getting the locally-saved source-html from the file system.

Revision history for this message
Best Alex Harrington (alexharrington) said :
#5

By default, IE embedded in Xibo is similar to the IE7 rendering engine. If
you want to use a newer version you need to set the registry keys up as
described here:
https://answers.launchpad.net/xibo/+faq/2246

Revision history for this message
Stephen Gower (socks-launchpad-net) said :
#6

Thanks! Realising it was in IE7 mode helped me twig that it's a lack of support built in for JSON.parse in my code above. Including json2.min.js in the code has made it all work nicely.