save the html once we call html through ajax - javascript

I want to call one html through ajax. once i get the response, i need to save that response as html on the spefied location
how can i do that?
I am calling ajax function using jquery as like below.
$.ajax({
type: "POST",
url: "../../../project/html/TC_print.html",
success: function(msg){
//once it success.. i need to save it as html on desktop
}
});
success call back, i need to save it as html on desktop

//once it success.. i need to save it as html on desktop
Forget about it. For security reasons, javascript that runs inside a browser doesn't have access to files on the client computer. Simply think of the consequences if this was possible. You probably wouldn't have been writing this question nor I have been writing this answer at the very moment as our computers would have been hacked badly. You visit a site and files start popping on your desktop. You visit a malicious site and viruses start popping everywhere, not only on your desktop.

JavaScript prevents you from saving files to a users computer for security reasons, You'd need to write the file to a server and then prompt the user to download the file by putting it in a ZIP or something similar.

I don't think that with javascript alone you will be able to save on the desktop but I might be mistaken. What I would do will to make another call from the success handler to a php script and pass in the html, then use PHP protocols to save file on desktop. Cheers.

Related

Load another HTML file inside dialog

I have a index.html and have another html file with a lot of text.
Now I don't want to paste this bunch of codes into my index.html.
I don't want to make it larger, the other file is not always called only if the button is clicked.
How do I get the div area from another_file.html ?
$("#buttonClick").on("click",function(evt) {
$("#load-another-file").dialog({});
Maybe you're looking for jquery.load(), load perform a server call and fetchs the HTML returned in your element.
$("#buttonClick").on("click",function(evt) {
$("#load-another-file").dialog().load("another_file.html");
});
I believe you are looking for jQuery's $.ajax, which is a way of loading data without refreshing the page:
$("#buttonClick").on("click",function(evt) {
$.ajax({
url: 'another_file.html',
success: function(data){ // You received the data
$("#load-another-file").html(data).dialog("open");
}
});
});
Note: for this to work, you might have to put your files on a server (even a local one), because some browsers don't allow Ajax requests on local files for security reasons (e.g. Google Chrome).

Easiest way to load and read a local text file with javascript?

I have a .csv file that I wish to load that contains information that the .HTML page will format itself with. I'm not sure how to do this however,
Here's a simple image of the files: http://i.imgur.com/GHfrgff.png
I have looked into HTML5's FileReader and it seems like it will get the job done but it seems to require usage of input forms. I just want to load the file and be able to access the text inside and manipulate it as I see fit.
This post mentions AJAX, however the thing is that this webpage will only ever be deployed locally, so it's a bit iffy.
How is this usually done?
Since your web page and data file are in the same directory you can use AJAX to read the data file. However I note from the icons in your image that you are using Chrome. By default Chrome prevents just that feature and reports an access violation. To allow the data file to be read you must have invoked Chrome with a command line option --allow-file-access-from-files.
An alternative, which may work for you, is to use drag the file and drop into onto your web page. Refer to your preferred DOM reference for "drag and drop files".
You can totally make an ajax request to a local file, and get its content back.
If you are using jQuery, take a look at the $.get() function that will return the content of your file in a variable. You just to pass the path of your file in parameter, as you would do for querying a "normal" URL.
You cannot make cross domain ajax requests for security purposes. That's the whole point of having apis. However you can make an api out of the $.get request URL.
The solution is to use YQL (Yahoo Query Language) which is a pretty nifty tool for making api calls out of virtually any website. So then you can easily read the contents of the file and use it.
You might want to look at the official documentation and the YQL Console
I also wrote a blog post specifially for using YQL for cross domain ajax requests. Hope it helps
You can try AJAX (if you do not need asynchronous processing set "async" to false. This version below ran in any browser I tried when employed via a local web server (the address contains "localhost") and the text file was indeed in the UTF-8-format. If you want to start the page via the file system (the address starts with "file"), then Chrome (and likely Safari, too, but not Firefox) generates the "Origin null is not allowed by Access-Control-Allow-Origin."-error mentioned above. See the discussion here.
$.ajax({
async: false,
type: "GET",
url: "./testcsv.csv",
dataType: "text",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
success: function (data) {
//parse the file content here
}
});
The idea to use script-files which contain the settings as variables mentioned by Phrogz might be a viable option in your scenario, though. I was using files in the "Ini"-format to be changed by users.

How can I create a .txt file using Jquery?

Ok, this is my domain: example.com/index.html and I want to create a .txt file into the domain. result: example.com/file.txt with this info:
.js:
$('.saveButton').on('click', function (e) {
e.preventDefault();
$('.HTMLcontentTosave').html(); //save this html content into a file.txt
});
$.ajax maybe?
Thanks a lot!
Using only jQuery/javascript in the browser, and a typically configured web server, this isn't possible. The filesystem of the server is not exposed in such a way that it is directly writeable from a client, that would be a rather large security risk.
To make it possible, you will need to employ some server side code, such as PHP to assist in writing the file on the server. At that point you can send the desired content, and name of the file as a request to the server side code and that code can then write it to your desired location on the server's file system.
Make sure to employ adequate protections so only certain (safe) files can be written to, by the certain users you specify, otherwise you could open the previously mentioned security hole.
Does this help you?
Saving a text file on server using JavaScript
Also, you can go through this one.
Technically, the "right" way would be to make a PUT request (the "type" argument: see http://api.jquery.com/jQuery.ajax/), but this is apparently not supported on all browsers, and some servers may also block these by default so you'd need to get around this. I say the "right" way because a PUT request is intended to mean that the consequence will be to save a file at the pointed location.
The more common approach would be to allow a POST request but you'd need a server-side script to do things like validate that a user had permission to make the request.

Save javascript output to a file?

I have php made website. It runs on windows machine. I have a javascript that i have tested that gets me the adobe version used by clients. The problem is that by running the code I'm able to retrieve the Adobe Reader version but cannot saved the output to a file on my server end.
Here is the Javascript code. I took this code from sister stackexchange website.
http://jsfiddle.net/EGbY5/3/
What is the best way to save this information from js? Should i use js or any other scripting? I read this from google that you cannot use js to save files on server? If this is true is there any other way.
I would suggest using AJAX (or a form, if you want the user to explicitly know this is happening) to send a request to your server side code detailing the information you have collected with the script.
The advantage of AJAX is that you can do all of this without the user being explicitly aware of it. The disadvantage is that you rely on their browser supporting Javascript, but since you are already collecting information using Javascript this problem is mute.
Ajax'll do it.
JavaScript source:
var adobeVersion = CheckAdobeVersion();
$.post("script.php", {version: adobeVersion}, function(){});
PHP source:
<?php
$version = $_POST['version'];
write all text to a file("my file.txt") // Pseudo code...
?>

jquery load to hide content

There is javascript on my webpage, but I need to hide it from my users (I don't want them to be able to see it because it contains some answers to the game.)
So I tried using Jquery .load in order to hide the content (I load the content from an external js file with that call). But it failed to load. So I tried ajax and it failed too.
Maybe the problem comes from the fact that I'm trying to load a file located in my root directory, while the original page is located in "root/public_html/main/pages":
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url : "../../../secret_code.js",
dataType: "text",
success : function (data) {
$("#ajaxcontent").html(data);
}
});
});
</script>
1) Why can't I load a file from the root directory with ajax or load method?
2) Is there another way around?
PS: I'm putting the file in the root directory so people can't access it directly from their browsers...
1) if the file isn't accessible via web browsers, than it's not accessible via ajax (ajax is part of the web browsers
2) try /secret_code instead of ../../../secret_code.js
What is your system setup? Are you using a CMS?
Even if you add the javascript to the page after page load a user with a tool like firebug can go and view it. I don't think what you are doing is really going to secure it. An alternate solution is that you could minify and obfuscate the javascript that you use in your production environment. This will produce near unreadable but functioning javascript code. There are a number of tools that you can run your code through to minify and obfuscate it. Here is one tool you could use: http://www.refresh-sf.com/yui/
If that isn't enough then maybe you could put the answers to the game on your serverside and pull them via ajax. I don't know your setup so I don't know if that is viable for you.
Navigate to the URL, not the directory. Like
$.ajax({
url : "http://domain.com/js/secret_code.js",
..
Even if you load your content dynamicly, it's quite easy to see content of the file using firebug, fiddler or any kind of proxy. I suggest you to use obfuscator. It will be harder for user to find answer
Take a look at the jQuery.getScript() function, it's designed for loading Javascript files over AJAX and should do what you need.
Try jQuery's $.getScript() method for loading external
Script files, however, you can easily see the contents of the script file using Firebug or the developer toolbar!
Security first
You can't access your root directory with JavaScript because people would read out your database passwords, ftp password aso. if that would be possible.
You can only load files that are accessible directly from browsers, for example, http://www.mydomain.com/secret_code.js
If it can't be accessed directly by the browser, it can't be accessed by the browser via ajax. You can however use .htaccess to prevent users from opening up a js file directly, though that doesn't keep them from looking at it in the google chrome or firebug consoles.
If you want to keep it secret, don't let it get to the browser.

Categories

Resources