-
Notifications
You must be signed in to change notification settings - Fork 41
Tri 2: Tech Talk 9.1 Google Search
jm1021 edited this page Feb 7, 2022
·
10 revisions
- Authors Billy, Sarah, Mr M
- Navigation Bar Code4
- This wiki page will contain step-by-step directions that will enable the reader to create a search function that uses the google search engine to search text on their deployed project.
Add HTML the NavBar
Explanation of Code
- Add input to capture search term
- Add id="search" for JavaScript event
<div class="px-3">
<input id="search" type="search" placeholder="Search" aria-label="Search">
</div>
Add JavaScript to NavBar script
Explanation of Code
- Add a function that executes when button pressed in Search, only do search when 'Enter' is pressed
- Setup a query by concatenating variables to support Google filtering
- This implementation opens a new browser tab that displays the results
Full code
const search = document.getElementById('search');
const google = 'https://www.google.com/search?q=site%3A+';
const site = 'https://nighthawkcodingsociety.com';
function submitted(event) {
if (event.key === 'Enter') {
event.preventDefault();
const url = google
+ site
+ '+'
+ search.value;
const win = window.open(url, '_blank');
win.focus();
}
}
search.addEventListener('keypress', submitted);