Yes I've looked already at internet, 3 different queries, 5 pages each, looked on StackOverflow, then its marked as offtopic question, searched through here, answer related like sun and pancakes.
My question is, how can I store content of file into variable?
While I was Googling I came on this website few times, and script did not work.
Could anybody help?
How can I store content of "foo.txt" into "xyz" variable?
(Sorry for such LQ post, just searching for an answer for hour, literally)
The only way I can think of doing it is to submit a file (via ajax or iframe) and then the callback or whatever method you prefer sets the value of a variable.
Javascript is abstracted from the OS/File System for security reasons. The user needs to "give" you the file before you can do anything with it. However, you are setting a "cookie" or local storage variable, you may be able to do something there. However, I think it would be fairly limited due to size contraints and the fact that you need to set the file before you can edit it.
Perhaps if you explained what you were trying to do someone can give a good starting point for research or code example?
If you want to read the file from the local machine, and you are good with using HTML5, then you can look at this article (just make sure your browser supports it). http://www.html5rocks.com/en/tutorials/file/dndfiles/
Why is everyone including ASP.net and Ajax?
There is file, a website file that I want to include to main page.
And I need content of file as variable to be able to change content by .js file.
This local file, but. But I mean JS in website design, not a file to double click on.
ActiveX objects, as far as I know, requires some sort of Microsoft intervention or emulation... that is, you probably need to do more than just create an active X object in script.
I assume, then, that you are running this in IE and on Windows, or are familliar with this type of object (I am not).
Looks like you are close. GetFile seems to require an absolute path. Try instead:
fileObj = fso.GetFile("C:\foo.txt"); //this assumes the file lives on the C:\ drive.
Hope that helps.
Alternatively, as I had suggested above (and others also tried to build upon), is you upload a file to your server using a traditional file input (<input type="file" name="upload" .... />), and let the server process the file for you, then spit out a return value that can be JSON encoded or any other encoding you like, and then store that in a javascript variable. This would be cross browser / cross platform, and allow you to do a bit more with the data coming in.
Are we surely talking about same Javascript? I don't need things to upload. The .js file is in same folder as .txt file. The script I want make work is:
function thefunction() {
document.getElementById("divid").innerHTML = content_from_file;
}
ok, use $.get instead http://api.jquery.com/jQuery.get/ or use load for a div that is hidden and when the load is complete put the contents of the div into a variable.
0
diafol
Why is everyone including ASP.net and Ajax
AFAIK you need to use Ajax to get the content, as previously mentioned. Avoid solutions using ActiveX as they will only work for IE.
If you just want the contents of a text file, then use something like jQuery (also as previously mentioned). A quick look gave me this...
I try to create a simple OOP PHP that shows an error:
class_lib.php
<?php
class person {
var $name;
function __constructor($person_name){
echo "Initialize class";
}
function set_name($new_name){
$this->name($new_name);
} ...