5,140 Pages

(Created page with "With pure JS, we end up doing: <pre> // For older browsers with lack of Object.keys() if (!Object.keys) { Object.keys = function (object) { var keys = []; for (var key in obje...")
 
 
Line 16: Line 16:
 
var url = query.pages[id].imageinfo[0].url;
 
var url = query.pages[id].imageinfo[0].url;
 
// Do something with url
 
// Do something with url
alert('URL:', url);
+
alert('URL: ' + url);
 
}).fail(function(error) {
 
}).fail(function(error) {
 
mw.log('API failed:', error);
 
mw.log('API failed:', error);
 
});
 
});
 
</pre>
 
</pre>
  +
Edit: fixed syntax error

Latest revision as of 22:30, April 11, 2014

With pure JS, we end up doing:

// For older browsers with lack of Object.keys()
if (!Object.keys) { Object.keys = function (object) { var keys = []; for (var key in object) { if (object.hasOwnProperty(key)) { keys.push(key); } } } }
$.getJSON('http://leagueoflegends.wikia.com/api.php?action=query&format=json', {
    //indexpageids: '', // Object.keys()
    titles: 'File:SonaSquare.png',
    prop: 'imageinfo',
    iilimit: 1,
    iiprop: 'url',
}).done(function(data) {
    mw.log('API result:', data);
    // undefined checks omitted for brevity
    var query = data.query;
    var id = Object.keys(query.pages)[0]; // Because titles only has one name.
    var url = query.pages[id].imageinfo[0].url;
    // Do something with url
    alert('URL: ' + url);
}).fail(function(error) {
    mw.log('API failed:', error);
});

Edit: fixed syntax error

Community content is available under CC-BY-SA unless otherwise noted.