document.write and JSON output - javascript

I have some big json configs and want allow users to export them. The problem is JS prompt cant return a full data, so i decided to use windows.open and write. But "write" just "eat" html in json values. This answer https://stackoverflow.com/a/22055706 helped a lot.
var data = Store.export();
var url = 'data:text/json;charset=utf8,' + encodeURIComponent(data);
window.open(url, '_blank');
window.focus();
But when i try to export really large and long json everything freezes...
How can i modify it to use blank url and unmodified json as text?
UPD
My workaround
var myWindow = window.open("", "JSON Settings", '_blank');
myWindow.document.write('<textarea>' + escapeHTML(Store.export()) + '</textarea>');
myWindow.focus();

You could include a multi-line text field in which you would place the data.
This gives it a space to go rather than just being injected into the HTML.
The user would then copy and paste the data out into their own .json file.

If you're trying to export the data, do you have any requirements prohibiting the user from downloading the data?
This lib has come in handy for me several times:
https://github.com/eligrey/FileSaver.js

Related

Saving URL path in the text file by using JavaScript

I would like to save/export my URL address to the text file (.txt).
Firstly I've managed with copying my URL address to the <textarea> by following this query:
How to copy URL on button click?
It works perfectly for me, although I need a bit more here. I need to have it exported to the text file.
Because I work on a local server, I use the get method in my form. It results in quite a long URL, which can be retrieved (reopened) when copied. I think it could be convenient for me to have it stored somewhere, like in the .txt file.
I used this solution:
https://www.websparrow.org/web/how-to-create-and-save-text-file-in-javascript
for saving dynamic data, although it doesn't work for me.
My code looks like this now:
<script>
function Copy() {
var Url = document.getElementById("url");
Url.innerHTML = window.location.href;
console.log(Url.innerHTML)
Url.select();
document.execCommand("copy");
}
function saveStaticDataToFie() {
var Urltext = document.getElementById("url").value;
var blob = new Blob([Urltext], {type: "text/plain;charset=utf-8"})
saveAs(blob, "survey.txt");
}
</script>
<button type="button" onclick="saveStaticDataToFile();">Click to Save</button>
I see no reaction at all. What have I done wrong here?

What is wrong with my javascript for a browser I would like to create?

I know you people will think I am insane, and thus an idiot, but I really have a serious question, well at least to me. What is wrong with my javascript for a browser I would like to create?It will not open the URL in the iframe. I am creating an html source file with some javascript to split a "get" url,and echo the resulting variable into an iFrame. I wanna use Webkit as the rendering engine, so I'm using Google Chrome to create an application shortcut. Yes, I know it will require google chrome, but this is just a test.
function goTo(){
var urlb = window.location.href;
var urla = urlb.split('localhost/browser.html?url=');
var urlc = urla[1];
var urld = urlc.replace("+"," ");
var urle = urld.replace("%3A ",":");
var urlg = urle.replace("%2F","/");
var url = urlg;
document.getElementByID('url').innerHTML="<iframe src=' . url . "'width='100%'
height='90%'></iframe> Opened:" . url ."</div>";
}
This is the javscript function to open the url. I am pretty sure you would think that the html is just a simple input form and the blank Iframe, which it is.
Please help me if you can.
When you are trying to get the URL, you want to use decodeURIComponent(). Don't make up your own function to unescape the data. Also, get the query string parameters properly. See this StackOverflow post: https://stackoverflow.com/a/901144/362536
Now that you have the proper URL, don't simply inject it into your HTML. You're opening yourself up to security troubles, and a broken browser when you run into characters you don't expect. Create the iframe, then set its attributes programmatically. https://stackoverflow.com/a/710347/362536
Finally, fix your syntax errors.

Download zip using jszip in Sharepoint

Hi I am looking for solutions insanely for almost 2 days.I have sharepoint library where multiple word docs uploaded.Using javascript JSZip I like to pass sharepoint file urls into it.Is this possible ?
I am getting each file server relative URL by ecmascript.Now I would like to do something like this:
function create_zip() {
debugger
var zip = new JSZip();
zip.add("http://myspsite/shareddoc/a/test.docx", "\n"); //this is hardcoded for testing??here I have file url
//zip.add("hello2.txt", "Hello Second World\n");
content = zip.generate();
location.href = "data:application/zip;base64," + content;
}
Any pointers or how I proceed from here?
Thanxs.
It´s insanely but interesting. Could works. Creating a button and calling the library and the script, I´m sure of that.
or, try this: http://techtrainingnotes.blogspot.com.ar/2010/05/sharepoint-2010-windows-explorer-view.html
This is more simple.

Create file to download js

I'm receiving a JSON output from an API, and i want to use it with JS and also save that output to file. Is there any way to trigger the browser download machanism using JS with thet output?
Maybe now some browsers support the attribute download but you don't trigger the browser to automatically download the file.
the only way i know is #BAS's solution but without a filename.
tested in chrome ..
array=[{a:'1',b:'2'},{x:'3',y:'4'}];
function dl(array,filename){
var b=document.createElement('a');
b.download=filename;
b.textContent=filename;
b.href='data:application/json;base64,'+
window.btoa(unescape(encodeURIComponent(JSON.stringify(array))))
// or
// b.href='data:application/javascript;charset=utf-8,'+JSON.stringify(json);
return b
}
document.body.appendChild(dl(array,'my.json'));
example
http://jsfiddle.net/8yQcW/
UPDATE direct download works ... on chrome i tested.
on append simulate click
var e=document.createEvent('Events');
e.initEvent('click',true,false);
document.getElementById('dl').dispatchEvent(e);
http://jsfiddle.net/8yQcW/1/
You could try it with a data URI:
function triggerDownload(json) {
var dataUri = 'data:text;charset=utf-8,' + json;
window.open(dataUri);
}
Try entering data:text;charset=utf-8,{"Key":"Value"} into your browsers address bar. It should show you the save dialog.
See here for more information and browser support.

How to download a file with a very long URL?

Basically I'm trying to trigger the download of a file which is constructed dynamically. In order to construct the file I need to pass a lot of information to the servlet.
This means I can't use GET, I should use POST instead.
In order for the download not to mess the application up, though, I must open a new window. The problem is that I cannot do this:
window.open(http://very.long.url.com/.../, "_blank");
since this would cause the URL limit to be exceeded and GET to be used instead of POST.
All information that needs to be sent to the servlet are in an object called "config.
My solution was to write a form in the new window with POST method, then add hidden inputs to it and then trigger the submit, like this:
var win = window.open("about:blank", "_blank");
win.document.write("<form method='post' id=donwload action='serveletURL'></form>");
$.each(config, function (name, value) {
var fragment = document.createDocumentFragment(),
temp = document.createElement('div');
temp.innerHTML = '<input type=\'hidden\' name=\'' + name + '\' value=\'' + value + '\'></input>';
while (temp.firstChild) {
fragment.appendChild(temp.firstChild);
}
win.document.getElementById("download").appendChild(fragment);
});
win.document.getElementById('download').submit();
And it works in Google Chrome and Firefox, but it doesn't work on IE. On IE (even IE 10), a download dialog with a "serveletURL" file name opens and nothing gets downloaded when I ask the file to be saved or opened.
I'm sure I'm missing something... How do I accomplish this??
Thank you!
Eduardo

Categories

Resources