Last active
April 18, 2018 15:04
-
-
Save bomboclat/b2c8e05fc6f09e05d4399ef661d8943a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$('select').formSelect(); | |
$("#action").on("click", search); | |
$("#search").keypress(function(e) { | |
if(e.which == 13) { | |
$("#action").click(); | |
} | |
}); | |
}); | |
function search() { | |
$("#random").hide(); | |
var searchString = $("#search").val(); | |
var language = $("#dropdown").val(); | |
var url = null; | |
if (language==2) { | |
url = "https://en.wikipedia.org/w/api.php" | |
} else if (language==1) { | |
url = "https://it.wikipedia.org/w/api.php" | |
} | |
$.ajax({ | |
url: url, | |
data: { | |
origin: "*", | |
action: "opensearch", | |
format: "json", | |
headers: { 'Api-User-Agent': 'Googlepedia exercise 1.0' }, | |
formatversion: "2", | |
search: searchString, | |
}, | |
success: function (result) { | |
$('#search-results').empty(); | |
for (i=0; i<10; i++) { | |
var title = result[1][i]; | |
var description = result[2][i]; | |
var url = result[3][i]; | |
$('#search-results').append('<div style="cursor: pointer;" class="card hoverable z-depth-2"><div'+ ' id='+[i]+' onclick='+'"window.location='+"'"+url+"'"+';"'+' class="card-content">'+'<span class="card-title">'+'<b>'+title+'</b>'+'</span>'+'<p>'+description+'</p>'+'</div></div>'); | |
}; | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment