Search code examples
htmlsearchtext

HTML link to a certain point in a webpage - slight twist


Here's the use case: A user clicks on a link that opens a window displaying the contents of a text log. Easy enough. But is there a way of using POST, to open that text log to a certain location (i.e., search for a particular timestamp given in the post, and show the file at that specific location)?

(Assume I can't put html tags inside the text log -- it's a raw file).

Template of log:

+++ 2009/06/19 10:47:12.264 ACTION +++
+++ 2009/06/19 10:49:12.111 ACTION +++

So I want the page to load a specific timestamp.

Thanks,
Michael


Solution

  • Since you can't modify the file, the only way would be to wrap it in a <frame> or an <iframe> and drive the searching and scrolling from JavaScript in the neighbouring/containing page.

    Here's an example, which you can try out online at http://entrian.com/so-container.html

    <html><head><script>
    function go() {
        // "line" is the <input> for which line to jump to; see the HTML.
        var line = document.getElementById('line').value;
        if (document.body.createTextRange) {  // This is IE
            var range = frames['log'].document.body.createTextRange();
            if (range.findText(line)) {
                range.select(); // Scroll the match into view and highlight it.
            }
        } else {  // Non-IE.  Tested in Firefox; YMMV on other browsers.
            frames['log'].find(line); // Scroll the match into view and highlight it.
        }
    }
    </script></head><body>
    <input type='text' size='5' name='line' id='line' value='10'>
    <button onclick='go()'>Go</button><br>
    <iframe name='log' width='100' height='50' src='so-data.txt'>
    <!-- so-data.txt contains the numbers 01-20 on separate lines -->
    </body></html>
    

    I've tested that in IE7 and FF3; I'd be surprised if it worked elsewhere without edits, but you never know your luck!

    Obviously in your case you'd be driving the scrolling programmatically rather than via an <input> box, but you can see how it would work for you.