document.getElementsByTagName() and specifying that the tag to obtain is the head tag.

Line 2: Next we want to create a script element and we can use the W3C DOM method of document.createElement. We use a variable named setjs, so that we can give it some attributes and then later slot it into the head section of the Web document.

Line 3: This line sets an attribute type with a value of text/javascript.

Line 4: We put the script tag and its attributes into the head section of the document using appendChild():

Line 5: We then want to fill the head section with the script itself, so we push the string of data received from the xmlhttprequest object into the head section.