Sunday, March 27, 2011

How can I get an HTML page to read the contents of a text document?

How can I get an HTML page (.html) to read the contents of a text document that can be found in the same folder as the .html file? The server is IIS.

Thanks

From stackoverflow
  • Google for server-side includes.

  • It seems like you can use #include directives in IIS.

    http://msdn.microsoft.com/en-us/library/ms525185.aspx

    But to be honest I strongly suggest using a scripting language, either PHP or something in the ASP family.

  • You can put an url to the text file directly, the user will download it by clicking. If this is not what you want, then you can probably use Server Side Includes (see here and here). If this does not work, you must write a script (ASP?) to read the file.

    borisCallens : I think you mean clicking instead of clocking. Prbly just a typo, but confused the hell out of me ;)
  • one hesitates to suggest iframes, but out of completeness...

    (You probably need server side includes, but you probably have bigger issues in general)

  • By adding the following JavaScript code to the element of the web page:

    <script>
    
    function clientSideInclude(id, url) 
    {
    
      var req = false;
    
      // For Safari, Firefox, and other non-MS browsers
    
      if (window.XMLHttpRequest)
     {
        try {
          req = new XMLHttpRequest();
        } catch (e) {
          req = false;
        }
      } else if (window.ActiveXObject) {
        // For Internet Explorer on Windows
        try {
          req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
            req = false;
          }
        }
      }
     var element = document.getElementById(id);
     if (!element) {
      alert("Bad id " + id + 
       "passed to clientSideInclude." +
       "You need a div or span element " +
       "with this id in your page.");
      return;
     }
      if (req) {
        // Synchronous request, wait till we have it all
        req.open('GET', url, false);
        req.send(null);
        element.innerHTML = req.responseText;
      } else {
        element.innerHTML =
       "Sorry, your browser does not support " +
          "XMLHTTPRequest objects. This page requires " +
          "Internet Explorer 5 or better for Windows, " +
          "or Firefox for any system, or Safari. Other " +
          "compatible browsers may also exist.";
      }
    }
    </script>
    
    Mark Brittingham : I upvoted to get rid of the downvote. Whoever downvoted this must provide an explanation (is the code wrong?). I *do* think that this is overkill and is not the answer that anyone would use when Server-Side includes are available. However, Ashutosh is providing some useful knowledge here.
  • IIS can do server-side includes. BUt, if you can't do this, and want to include the text file in the HTML, you could grab the file with an XMLHTTPRequest object and insert it into the DOM with Javascript.

    Naturally, a JS library will make this easier. For example in prototype:

    new Ajax.Updater($('id_of_div'), 'http://yourdomain/path/to/file.txt');
    

    that would grab the file and drop the contents into <div id="id_of_div"></div>

    Or in jQuery:

    $("#id_of_div").load("http://yourdomain/path/to/file.txt");
    
    zoul : I think this should only be used by people who know what they’re doing. Something as simple as including a file should work even without JavaScript. Just an opinion.

0 comments:

Post a Comment