in the following javascript codes follow the directions below and modify them sc
ID: 3861126 • Letter: I
Question
in the following javascript codes follow the directions below and modify them
script.js
/* JavaScript 6th Edition
Chapter 11
Hands-on Project 11-1
Author:
Date:
Filename: script.js
*/
"use strict";
search.htm
<!DOCTYPE html>
<html>
<head>
<!--
JavaScript 6th Edition
Chapter 11
Hands-on Project 11-1
Author:
Date:
Filename: search.htm
-->
<meta charset="utf-8" />
<meta id="viewport" content="width=device-width,initial-scale=1.0">
<title>Hands-on Project 11-1</title>
<link rel="stylesheet" href="styles.css" />
<script src="modernizr.custom.65897.js"></script>
</head>
<body>
<header>
<h1>
Hands-on Project 11-1
</h1>
</header>
<article>
<form>
<fieldset>
<input type="search" />
<input type="submit" value="Search" />
</fieldset>
</form>
</article>
<script src="script.js"></script>
</body>
</html>
1. In the script.js file, after the “use strict” statement, declare a global variable named httpRequest with a value of false. Below the variable declaration, create a function named getRequestObject() that contains the following try/catch/return statements:
try {
httpRequest = new XMLHttpRequest();
}
catch (requestError) {
return false;
}
return httpRequest;
3. Below the getRequestobject ) function, add the following getResults ) function: var newP2 = document.createElement("p"); var newP3 = document.createElement("p"); var newA = document.createElement( "a"); head.appendChild (newP1); newA.innerHTMLitems [i] Title; newA.setAttribute ("href", items[i].Url) newP1.appendChild (newA) newP1.className = "head". newP2.innerHTMLitems [i] Url; newP2.className-"url"; newP3.innerHTMLitems [i].Description; newDiv.appendChild (head)i newDiv.appendChild (newP2) newDiv.appendChild (newP3) articleEl.appendChild (newDiv) 12 1 unction getResults (evt) 13 if (evt.preventDefault) evt.preventDefault oi else 15 evt.returnvalue = false; 18 var entry document.getElementsByTagName("input")[0].value; f (httpRequest) httpRequest getRequestobjecto 20 10 21 12 13 14 15 httpRequest.aborto httpRequest.open ("get","search.php?q"entry, true) httpRequest.send ) httpRequest.onreadystatechange displaySuggestions; 23 24 25 26 27 28 4. Below the getResults() function, add the following displaySuggestions() function: 1 function displaySuggestions) if (httpRequest, readyState = 4 && 5. Below the displaySuggestions) function, add the following code to crea httpRequest.status200) SearchResults JSON . parse (httpRequest. responseText); var items searchResults.d.results: var articleE! document.getElementsByTagName- event listener 1 var form- document.getElementsByTagName("form") [0) 2 if (form.addEventListener) ("article") [0]: form.addEventListener ("submit", getResults, false) for (var i - 0; iExplanation / Answer
The modification to be done is in script.js file.
In task 1, you're told to declare a global variable.
So, when you declare a variable outside a function, it means it is a global variable which can be accessed by all the functions in the script file.
So, you simply have to write var httpRequest = false in order to create a global variable with value false.
After that, in the same task, you're supposed to write a function named getRequestObject().
The content inside it is already given.
So adding the following function...
function getRequestObject()
{
try {
httpRequest = new XMLHttpRequest();
}
catch (requestError) {
return false;
}
return httpRequest;
}
So, task 1 is completed now...
Tasks 3-5 does not require anything to add in the given code. You simply have to copy the code after the code we got in task 1.
So, the code for script.js will be...
script.js
/* JavaScript 6th Edition
Chapter 11
Hands-on Project 11-1
Author:
Date:
Filename: script.js
*/
"use strict";
var httpRequest = false;
function getRequestObject()
{
try {
httpRequest = new XMLHttpRequest();
}
catch (requestError) {
return false;
}
return httpRequest;
}
// After this copy the code from tasks 3-5.
Do comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.