Link to home
Start Free TrialLog in
Avatar of andrewaiello
andrewaiello

asked on

Bypassing Local File Security allowing javascript to read in a file

Hello All,

      I have an html file using javascript to read in a file: this does not work when I browse locally to the file because of security Issues.   The error that I get is as follows:  


--------
Error: WeekLunchStart is not defined
Source File: file:///C:/lcd/testSchedule.html
Line: 85
--------

Now, this WeekLunchStart variable is defined in a text file that is read in with the javascript:

getFile('testValues.txt');          where this function is defined earlier as:

-------------------------
function getFile(pURL) {
   if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc
      xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=postFileReady;
      xmlhttp.open("GET", pURL, true);
      xmlhttp.send(null);
   } else if (window.ActiveXObject) { //IE
      xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
      if (xmlhttp) {
         xmlhttp.onreadystatechange=postFileReady;
         xmlhttp.open('GET', pURL, true);
         xmlhttp.send();
      }
   }
   
}

// function to handle asynchronous call
function postFileReady() {
   if (xmlhttp.readyState==4) {
      if (xmlhttp.status==200) {
            eval(xmlhttp.responseText);
             document.getElementById('theExample').innerHTML=xmlhttp.responseText;
             
      }
   }
}

------------------

I tried overriding the security issue with:  https://addons.mozilla.org/en-US/firefox/addon/281  

as well as modifying the user.js firefox preference file to try to allow it:  

in my profiles folder at:  C:\Documents and Settings\Administrator\Application Data\Mozilla\Firefox\Profiles\f7yd3r6e.default     I created a text file called user.js     with the information:

user_pref("capability.policy.policynames", "localfilelinks");
user_pref("capability.policy.localfilelinks.sites", "file:///C:/lcd/testSchedule.html");
user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");


It still doesn't work; did I do something incorrect?   I need to have this working by the end of the day or its my head.  Please help; and thank you!

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

What do you try? Do you want to read local file file:///C:/lcd/testSchedule.html
Is your page also local, then it will work. From web server page it will never work.


Avatar of andrewaiello
andrewaiello

ASKER

no:  the file:  file:///C:/lcd/testSchedule.html is reading in the file  testValues.txt    which resides in the same location (file:///C:/lcd/testValues.txt)     If i am accessing this via a web server  (as in  http://localhost/lcd/testSchedule.html)  it will read in testValues.txt no problem; but browsing locally (which i need) doesn't work.   It works in IE but it throws a security flag; i hear that firefox blocks this behavior, but there are ways to get around it: thats what i am trying desperately to do.
This works for me:



<html>
<head>
<title>Zvonko &#42;</title>
<script>
window.onload = function(){
  getFile('testValues.txt');          
}

function getFile(fileName){
  window.frames.textFrame.location = fileName;
}

function postFileReady(){
  if(window.frames.textFrame.document.body.innerHTML){
    var responseText = window.frames.textFrame.document.body.innerHTML;
    responseText = responseText.replace(/<[^>]+>/g,"");
    document.getElementById('theExample').innerHTML=responseText;
    eval(responseText);
    alert(WeekLunchStart);
  }
}
</script>
</head>
<body>
<iframe name="textFrame" onLoad="postFileReady()" height="0" width="0" ></iframe>
<div id="theExample"></div>
</body>
</html>



Hmm;  this seems to work around the security issues... but I think I am having a problem integrating it into my file because it needs to keep being called on a timer.

I am trying to get it working myself; but if you can could you take a look at the source of this page:  https://ssl117.alentus.com/jkingsweb/gbt/PaxTest/TestLocal/testSchedule.html

And see if you can help me integrate your code into mine (i tried just swaping the two functions but that didn't work).   I'll work on it myself too; but if you could help I'd be super in your debt;  I need this working in an hour or I am dead!!   Thanks!!!
Also note that in my file i dont need to display the values that are read in; they are used for further javascript calculations.
I think the thing that is messing me up with trying to integrate it is the Asynchronous calling of the postFileReady Function that needs to happen in my code.
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you send me your phone number to the email address in my profile?
Hmm, its weird;  that worked the first few times I tried it; but now its not redirecting anymore.    

Want my phone #?   Gonna call me and tell me what a bad programmer I am, lol  ?
I do not see what you are doing. On your site I see still the same old errors.
Oh weird:  it only works if this is not commented out:

<div id="txt"></div>
 <div id="theExample">Loading...</div>

 
I guess i'll just have to make that invisible or something if I dont want the text to show up for that brief second.
Its okay; it works.  All i needed was to make the page redirect to another page based on what time it was and the values in that text file.  It now does this.  Thank you so much; you really saved my hide!!!
:-)