1

The following code is launched from www.example.com and gets the full html source code of www.example.com/example.html and alerts it.

function process(){
    url = "http://www.example.com/example.html"
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
    if (xhr.readyState == 4){
        alert(xhr.responseText)
        }
    }
    xhr.send();
}
process();

Now what i want to do is get the email from the html source code which is like this:

<input type="hidden" id="email2" name="email2" value="example@example.com" />

Usually if i were on the page itself i'd just do a simple:

document.getElementById('email2').value

But in this case how do i simply parse the email into a variable from the variable containing all that html source code which is:xhr.responseText?

Tom
  • 75
  • 1
  • 1
  • 12

3 Answers3

0

you cant use getElementById.. because all you have is a string ..but you can parse it

alert(xhr.responseText.split('id="email2"')[1].split('value="')[1].split('"')[0])
gezzuzz
  • 188
  • 2
  • 16
  • That doesn't work because you assume in that .html page there is only 1 line, it's at least 500 lines. – Tom Jul 25 '13 at 04:17
  • I replaced alert(xhr.respondText.split) with that line and i got no alert. – Tom Jul 25 '13 at 04:26
  • try... and tell me the output .........alert(xhr.responseText.split("email2")[1]) – gezzuzz Jul 25 '13 at 04:32
  • I just get undefined. – Tom Jul 25 '13 at 04:36
  • the problem is that xhr.responseText is unprocessed source... which can be different from the source you see in the browser.. would need to see the page to know the real problem...alert(xhr.responseText.split("email2")[1]) is undefined then your xhr.responseText does not contain email2 anywhere.. – gezzuzz Jul 25 '13 at 04:38
  • How can i output all the content of xhr.responseText to see what it actually contains? The alert box is too big. console.log(xhr.responseText) doesn't work. – Tom Jul 25 '13 at 04:41
  • document.body.innerHTML = "" – gezzuzz Jul 25 '13 at 04:45
  • Sorry, my mistake it actually worked. One last detail missing, how do i set xhr.responseText.split('id="email2"')[1].split('value="')[1].split('"')[0] as a variable? I tried test2 = xhr.responseText.split('id="email2"')[1].split('value="')[1].split('"')[0], then after the end of that function i tried to do alert(test2) and nothing alerted. – Tom Jul 25 '13 at 05:34
  • outside you function put var test2 = "" otherwise it would be treated as a local varible and drop after the function completes – gezzuzz Jul 25 '13 at 05:38
  • I did that and now it's an empty alert box. Here's my code: http://pastebin.com/0YHHgwQ3. – Tom Jul 25 '13 at 05:52
  • ahhh.. the issue is you are doing the alert before the page is loaded.. look at my new answer – gezzuzz Jul 25 '13 at 12:16
0

What you can do is create a HTML document based on the response of the xhr call and then query that (using getElementById or other DOM functions).

See How to create Document objects with JavaScript for how to create a new HTML document.

Community
  • 1
  • 1
andrewb
  • 2,995
  • 7
  • 54
  • 95
0
test2 = ""
function process(){
    url = "http://www.example.com/example.html"
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
    if (xhr.readyState == 4){
        test2 = xhr.responseText.split('id="email2"')[1].split('value="')[1].split('"')[0]
    process2();
    //do stuff here
        }
    }
    xhr.send();
}
process();

function process2(){
    // or do stuff here
    alert(test2);
}
gezzuzz
  • 188
  • 2
  • 16