Hello, I'm looking for some help here. The problem is I'm not very good with javascript, I try to avoid using it as much as I can, but ther's a project that I'm working on that requires some things done specifically by javascripts. The problem is very basic, but hard for me as I dont really understand this language: I would be very grateful if someone would write me an example of a function that would fetch content from a file in the web (e.g. http://something.com/something.txt) and write it to a variable. The file would consist only of a few simbols. Thank you in advance for any answers
Here it is in PHP: <?php $variable = file_get_contents('http://www.google.com'); echo $variable ?> PHP:
Thanks, but I know how to do this on php, the problem is I MUST do that using javascript. Unless you know how to make firefox extensions work with php ^^
I am not well versed in javascript file functions, but may be using XML data helps you. You could see http://www.devarticles.com/c/a/JavaScript/JavaScript-and-XML/ to see of thats helpful.
function fetch_file(url) { var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your browser does not support XMLHTTP!"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { var file_content = xmlhttp.responseText; } } xmlhttp.open("GET",url, true); xmlhttp.send(null); } Code (markup): i mashed it from w3school tutorial, so maybe its buggy, but the general solution is using ajax to fetch the file..
You will not be able to use javascript to fetch a file from a remote server. The file that you are required to fetch must exist on the same server as the javascript that is trying to fetch it.