1

I have a folder with many HTML documents, named 001.html to 999.html. In each document, there are the following lines:

<span class="my-name">Name X</span>

I want access to the content of this <span>, which I can get by opening the document in my browser and then entering the following in my addressbar:

javascript:document.getElementById('my-name').innerHTML;

Without altering the HTML documents, and without having to do it manually, how can I write a script which loads these HTML documents externally, applies the javascript above and returns the content of the <span>?

5
  • It's not possible without altering the HTML, you would need to include the script on each of these pages. Commented May 5, 2015 at 20:49
  • Can I not write a new HTML document with some script like: myurl='001.html'; myurl.getElementById('my-name').innerHTML; or something? Commented May 5, 2015 at 20:51
  • Otherwise, perhaps using PHP? Commented May 5, 2015 at 20:54
  • No, because only that page would have access to that function. You would either need to place the script into each individual HTML page or use PHP to create a header file that loads a single file containing the script and include it on each page, either way you would have to alter every HTML file. Commented May 5, 2015 at 20:54
  • If this is just for your own use, another option would be to create a macro to visit each of those pages and run the script the way you're doing it currently. Commented May 5, 2015 at 20:56

2 Answers 2

1

Run this from an HTML file in the same folder. Code is not tested:

function read(i, toIncl, done, result, iframe) {
    if (i <= toIncl) {
        if (!result) {
            result = [];
        }
        if (!iframe) {
            iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            iframe.onload = function () {
                result.push(iframe.contentWindow.document.querySelector('.my-name').textContent);
                read(++i, toIncl, done, result, iframe);
            }
        }
        iframe.src = ('000' + i).substr(-3) + '.html';
    } else {
        iframe.parentNode.removeChild(iframe);
        done(result);
    }
}

read(0, 999, function (result) {
    console.log('Result:', result);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Appreciate the effort.
0

Sounds like you need some kind of HTML parser. HTML Agility Pack is a good option. You can read each individual file, search for the span, and print out the result. JavaScript is not enough for what you are trying to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.