0

I have the task of writing JavaScript code to extract the text from an external web page and count the number of occurrences of each word in the text. I am also given these two assumptions:

You may assume that the web page will be on the same file system as the web page written for the exercise.

You may also assume that the web page comprises correctly-formed XHTML

I've worked out from some similar posts on this site how to get the text from the html using the .textContent and .innerText.

I want the user to be able to specify the webpage in a text input.

What I don't understand is getting the other html document in some sort of way so that I can get the text and parse it.

Chaz
  • 195
  • 4
  • 17

2 Answers2

0

use jQuery.load()

 var targetDiv = document.getElementById('my-div');
 var input  = $("input");
 $(targetDiv).load(input.value);
Nir Ben-Yair
  • 1,566
  • 3
  • 14
  • 18
  • 1
    `jQuery` isn't tagged in this question so I think it would be best you explain that you must link to the `jQuery` library to use it. – NewToJS Feb 12 '17 at 12:48
  • I've never used jQuery although I know a small amount. What is the second line of your code doing? Getting the text input? – Chaz Feb 12 '17 at 13:02
  • it is caching the $("input") selector – Manos Kounelakis Feb 12 '17 at 13:04
  • Yep. Sorry that I havn't noticed jQuery is not tagged in here, I would recommend to use jQuery for that. jquery has the load() method exactly for this purpose and on the second line what I'm doing is just grabbing the text input. – Nir Ben-Yair Feb 12 '17 at 14:13
0

Executing javascript in someones browser means you are telling the user to do something for you. To prevent you using that someone to load a completely foreign page for yourself is something limited by security reasons to protect the user and the external site. If that foreign site is allowed you to download / parse their content then jquery.get is enough for this.

Bulent Vural
  • 2,630
  • 1
  • 13
  • 18