0

Searching and reading here got me part way to my solution, which is a workaround for loading a text file using oft recommended code such as:

function FileRead(U) 
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        X=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        X=new ActiveXObject("Microsoft.XMLHTTP");
    }

  X.open('GET', U, false );
  X.setRequestHeader('Content-Type', 'text/html')
  X.send();
    return X.responseText;
}

This works well for me, so far, on PCs, but not on Macs.

So I used a technique recommended here where I designate the local file as a source in a script, and that worked well for several pages. For the other pages I need to define the source at runtime, taking the path from local storage. Again, tips here helped, but I hit a snag. The following code, located in the header, gets thing rolling.

<script>
    var albumPath = '';
    if (typeof(Storage) !== "undefined") 
    {
        albumPath = localStorage.getItem('photo_album_path');
    }
    if (albumPath.substr(albumPath-1,1) !== '/') {albumPath = albumPath +'/'}

    var Raw_Album_Info = '';
    var script = document.createElement('script');
    script.src = albumPath+"album_info.txt";
    document.head.appendChild(script);
</script> 

<script src="../scripts/common.js" type="text/javascript"></script>
<script src="../scripts/photos_pages.js" type="text/javascript"></script>

<script>alert(Raw_Album_Info);</script>

The code above does not work unless I include the final "alert" line. It displays a blank message, but it works. If I add a second alert statement it shows the contents of the source file.

At the bottom of the page I have this:

<script language="JavaScript" type="text/javascript">
window.onload = LoadAlbumInfo();
var slideIndex = 2;
showSlides();
</script>

LoadAlbumInfo parses the file and draws content on the page.

Clearly there is an onload-type issue going on here but I can't figure it out. Any suggestions?

Bump on Feb 11. Still looking for a solution.

I tried everything I can think of, from setTimer to moving the code around, to a second window.onLoad statement, and the only thing that gets this to work is inserting an alert statement in the code anywhere between where the script is created and when the script element is used.

If I can't figure this one out I can always resort to putting 8-10 identical html files in different folders, but that would be a pain to maintain, and not pretty.

As for using php etc, this project is going on a thumb drive. It is stand-alone. No server, no Internet.

10
  • 1
    Whatever site or resource you're looking at for help is showing you very old code. Any browser released in the last 8 years has native support for XMLHttpRequest(), so there is no need for the if/else statement you've created - - just make an instance of the object. Also, language="JavaScript" type="text/javascript" hasn't been needed on script tags for many years as well. You can remove that part. Commented Feb 7, 2019 at 23:06
  • IE5, IE6 <- you sure you want to handle these? likely not; remove! Commented Feb 7, 2019 at 23:08
  • I'm a bit confused why you're loading a text file into a script element. Why not just use ajax (XMLHttpRequest) to get the contents? Also, loading any file from the server is asynchronous, so it may not be loaded by the time your function is called. Better to add an onreadystatechange event handler to XMLHttpRequest, check the state and run your function when it is completely loaded. Commented Feb 7, 2019 at 23:15
  • The code that you say works well for you is incorrect and likely isn't working correctly at all. You haven't set up a "success" callback for your XHR component and are attempting to get the .responseText immediately after sending the request, which would never really work. You need to place that in the success callback. See this for how to use an XHR. Commented Feb 7, 2019 at 23:15
  • This first snippet has worked for me for years, but I'd never tried it on a Mac until now. That snippet does make an XMLHttpRequest but a Mac this was tested on last night objected to it. Many other posts on this site confirmed it could be a problem, that local file access is generally blocked. A solution suggested was to make it a script element, which works for fixed file names but is not ideal because I'll have to edit all of my txt files. It works for variable file names, too, but not correctly, not without the "alert" statement. Commented Feb 7, 2019 at 23:44

0

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.