access a certain line from a file and change it using javascript - javascript

I have a XML File and I need to access that file in Javascript.
I need to get a certain line and change it.Here is an example:
changeFileCode("data.xml", 12, "New Data");
// The first paramater is for the file we want.
// The second paramater is the line we want.
// The third paramater is the new content we want written
Is there a way to do it?
P.S.: I don't want a piece of code that has 1000 lines.

You should not rely on Line/Columns numbers in XML. because XML neglect white-spaces.
But you can target a specific node and change its value using JQuery.
Let's say you want to do that from a client-side, because that's what javascript do, you will have to get that XML file from the server, in purpose to edit the particular parts then send it back to the server to save it; for example:
$.get("yourGetUrl.php", function(data) {
var xml = $(data);
xml.find("yourNode").text("yourNewData");
$.post("yourPostUrl.php", xml, function(resp) {
alert(resp);
}, "xml");
});

Javascript cannot directly access the local file system. The safe way to do this would be to get the user to provide the file via an
<input type="file"/>
element.
You can then manipulate the file using the File Api
The user will then need to re-save the altered file.
The other way to achieve this would be to write a server side api using whatever technology you fancy (php/asp etc) to manipulate files based on parameters and then call it using ajax from your web page.

Related

How can I get information from a local text file without browsing?

So what I'm trying to do is get text from a file in the same directory as my html file using JavaScript. I want to store an array inside a text file and change it whenever i want instead of constantly having to go into the code, save it, check if it works etc.
I've tried looking around but couldn't find any clear information, most of what I found is using .readAsBinaryString, etc..
I'm mostly seeing things like this but i can't seem find anything which is actually getting information from a textfile without making the person find the text file directory.
function storearray(newval){
var file = "file location;"
var txt = file.txt;
var array = txt.split("|");
txt = txt + newval + " | ";
return array;
}
To read a file from the user's disk you need to use FileReader and the user must explicitly select the file using a file input. (See JavaScript read file without using input).
To read a a file from the website you need to use Ajax (with fetch, XMLHttpRequest or a library that wraps around them like Axios). (See Using fetch from MDN).
If (as it seems here) you want to read data from the website but the website exists only on the user's disk then you still need to use Ajax but will usually run into security restrictions. Some browsers allow you to disable the security protection, but the general solution is to install a web server and load both HTML and the data file using HTTP.
Alternatively, you can store your data in JavaScript (you are generating an array from your text file, you can so that manually or have a build-time script do it) and just load it with a <script> element.

Write a line to javascript file

Im using a array to display some images in a website:
var paintingImages;
paintingImages =
[
{
url: 'images/objects/ron.jpg',
alt: 'ron'
}
];
This js code is written in paintings.js and my main js code is written in the file main.js
I have made this website for a artist and I want to give him the opportunity to login and add pictures to his website. I'm not that good with php, but adding images to a ftp in a folder is no problem.
Because I'm using a array for retrieving the images, I need to be able to add items to the array.
This is the part where I'm stuck. I don't know how to edit a existing js file, so the next time I open the website, the items (images) will been shown.
Activexobject is not a option because it's only possible in IE.
In summary:
I need to add a item to a array
I need to save this into the file, so the next time the website opens, it will be shown
I'm not verry good with php, I prefer javascript
Can't use Activexobject because of the use of multiple browsers
You can not simply modify a javascript file sitting on the server from browser side javascript.
You need to implement some server side logic.
If you don't like PHP, but like JavaScript, check out NodeJS for example.
With Node you should be able to build some lightweight serverside logic to modify your json array file with additional images.
You would need to change your logic .. You need to send this data to server anyhow . and when you load your next time take it back from server and update your webpage accordingly. I would suggest use any javascript MVC framework like backbone, angular.js. it will help you.
Use a database.
Save it to a database server (You figure this out, could be via forms, xhr, websockets, etc.).
On page load, load the correct data for the current user.
Create the $userPaintings array (call it what you want) from the user data in PHP.
Then simply output a JS object in the page with PHP:
<script>
var paintingImages = <?php json_encode($userPaintings); ?>
</script>

Best way to store data to be read by JS

I am new to Javascript and HTML5.
What is the best way to store text data to be read in JS.
I want to load all the characters of a file into an array, the file is similar to a map file and is stored amongst my resource file.
In Java I would create a text file and read each byte into an array, what is the equivalency of doing that in JS? Does JS have a better method of doing this?
Best way - create .js file with JSON encoded data. Almost all languages have functions for this, json_encode in PHP for example.
You can create a file like:
window.data = {something: true, fromfile: ['f','d','s']};
and include it as javascript file on html page:
<script src="data.js"></script>
So you'll have that array in window.data.
Or create a file:
{something: true, fromfile: ['f','d','s']}
And load it with jQuery's AJAX:
$.post('fileURL',{}, function(data) { console.log(data); }, 'json');
and get it at any moment you need it. You can also generate such data at any time with server-side scripts to get actual data, just change fileURL.js to server-side script's URL.
You can also change last parameter (json) to 'text' and get file content as string. But I'd not trust it for binary files, better to convert it on server side to JSON.

Change an XML without changing it in server

I've got a llist of videos, with a click on a name it should display a video, the video-player i'm using read the video file name from a XML document in the same folder, I was thinking in, change the name of the file with javascript in the xml when a video name is clicked but I think this would change the original XML from the server and make it imposible for two people to view the page at the same time which actually sucks.
So is there any way to change only the XML on the user computer?
Or is there another way you can think to acomplish this job?
You can't change a (xml) file on the server easily. When you load it, the server will send you a stream of characters (call it string), which the browser will parse into a Document Object Model. This model is obvious locally, and when you modify it by using DOM manipulation methods like setAttribute, no one else will be affected.
To change a file on the server, you would need to explicitly request the server to do that.

get a mutating url with javascript

i have the following question:
i'm currently working with a software(MicroStrategy, BI) wich has a functionality that exports reports to pdf, it works something like this:
each report has an unique ID, so you select the report to export, and with jsp i send this report's id to the exporting tool, and it generates a complete URL with some parameters that the MicroStrategy server will read to generate the PDF.
What i'm trying is to capture this pdf url and send it to a Java method that will save this pdf in the hard drive without prompting anything to the user.
My problem is that this URL doesn't generate instantly, it takes a while, AND, some redirections are made in the process.
So, after all that chitchat, how can i capture that damn URL?
What i'm doing is making the pdf load into an iframe, and then extracting the url with a js code i found searching, assigning it to a JSP variable, and then, once i have the pdf url, call the Java Method. But it is not working.
The JavaScript function is this:
<script language="text/javascript">
function getSrc()
{
var CurrentUrl = document.getElementById('miframe').contentWindow.location.href;
if(currentUrl.substr(length-5)==".pdf")
{
return currentUrl;
}
else
{
setTimeout(getSrc(),5000);
}
}
</script>
and this is the call i make to it:
<% jsp code
String currentUrl="<script>document.writeln(getSrc();)</script>";
more jsp code %>
The rest of the code is actually fine, tried it with a normal pdf URL and it saved the pdf into the disk.
Hope it is understadable, and thanks in advance!
Your main problem is that you are calling getSrc, not passing it to setTimeout (you are actually passing null to setTimeout, unless the second call to getSrc happens to work, in which case you are passing a string, which setTimeout can't process due to "syntax errors".
Instead, use setTimeout(getSrc,5000); - no parentheses after getSrc. This passes the function, rather than its result.
Also, currentUrl.substr(length-5) is wrong, partly because length is undefined (you need currentUrl.length in there), and partly because you need -4 to get the last four characters.
I don't know what kind of access you have to MicroStrategy, but there is a MicroStrategy java api that will allow you to execute the document and get pdf without capturing the url.
Check out their Knowledge Base for examples.
Why don't you just save the report/document with PDF format as default, in this way when you open the report it will automatically generated in PDF.
If you don't like the idea to save a report in PDF (for example because you need it also as regular report and you don't want to maintain two version of the same object), you can use URLAPI to generate the PDF using &executionMode=3 and &currentViewMedia=32.
Not sure about these parameters, the best way for you to figure out which they are (beside some MicroStrategy TN) is to export the report in PDF and check the url.

Categories

Resources