Search wikipedia
// JavaScript Document
let resultsList = document.getElementById('resultsList');
let searchForm = document.getElementById('searchForm');
let searchInput = document.getElementById('searchInput');
searchForm.addEventListener('submit', (e)=> {
e.preventDefault();
displaySearchResults(searchInput.value);
})
function displaySearchResults(x){
let url = `https://en.wikipedia.org/w/api.php?action=query&list=search&prop=info&inprop=url&utf8=&format=json&origin=*&srlimit=20&srsearch=${x}`;
console.log(url);
fetch(url)
.then(function(response) {
return (response.json());
})
.then(function(data){
let resultsArray = data.query.search;
resultsOnPage(resultsArray);
})
.catch(function () {
console.log('An error occured');
});
}
function resultsOnPage(myArray){
resultsList.innerHTML = " ";
resultsList.insertAdjacentHTML('beforeend', `<h2>Search Results for ${searchInput.value} </h2>`);
myArray.forEach(function(item){
let itemTitle = item.title;
let itemSnippet = item.snippet;
let itemUrl = encodeURI(`https://en.wikipedia.org/wiki/${item.title}`);
resultsList.insertAdjacentHTML('beforeend',
`<div class="resultItem">
<h3 class="resultTitle">
<a href="${itemUrl}" target="_blank" rel="noopener">${itemTitle}</a>
</h3>
<p class="resultSnippet"><a href="${itemUrl}" target="_blank" rel="noopener">
${itemSnippet}</a></p>
</div>`
);
})
}
5
1
"The only true wisdom is in knowing you know nothing.",
Not even wikipedia is gonna change that
Thank you!
1
0
0
0
"The only true wisdom is in knowing you know nothing.",
Not even wikipedia is gonna change that
Thank you!
0
0
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line