javascript update via external xml or txt possible? - javascript

I have a javascript countdown. The first line is:
dateFuture1 = new Date(2011,8,21,20,30,0);
is it possible to update the date and time values via external text file or XML feed and if so, what would it look like?
Would something simple as
2011,8,21,20,30,0
inside a text file be possible?
Thank you in advance

You would need some method of accessing the values within the text or XML file, but yes it is possible. I would suggest looking into the ajax() function within jQuery as it is fully capable of handling your request. You can also utilize JSON to help pass values to the client from the server, which in my opinion is the best way to go.

Related

Transfer big data to php server-script

Currently I´m creating a little IDE in Javascript on my webpage and I´m struggling at the concept of the saving process. For the beginning I want to save the whole code written in my IDE to be saved in a .txt.
So far I know how to pass values from client-JS to server-PHP with HTTP-request, but my case I basically have to transfer a very long string. From within the php script I want to create/manipulate a .txt to save the data outside of the root of the webserver.
So I was wondering if there is a good solution to pass a lot of data at once to my php running on the server.
Do you mean something like this?
https://www.w3schools.com/php/php_file_create.asp

how to parse .asn file to managable object in javascript or python

I am creating a website which can upload .asn file, fill the parameter, send the request and show the response in html.
I am thinking that I could use javascript to parse the .asn file in order to show the imput and the response result.
But I am having trouble deal with this.
Do I have to write my own parser to do it or is there any tools?
I will appreciate any help, thanks.
update:
I am using flask with javascript so that a python solution can also solve this problem
I finally have a solution to this, This python package: https://github.com/kimgr/asn1ate
create a semantic tree so that I can somehow turn it to json form to diaplay.

How to modify HTML from another file with JavaScript/jQuery

I need a reference of a HTML element that has an id of #searchResults
$.get('search-for-prospect', function() {
_content.find('.prospect-container').sort(function(a,b){
...stuff...
}).appendTo('#searchResults');
})
I tried using jQuery's get to get the that element, but it doesn't work as expected.
I need to get a reference of searchResults and append to it. How can I achieve that?
The only way to get HTML from another page with javascript is by making AJAX call (in fact js template engines work this way).
So you have to $.ajax the page you want, parse it as HTML and do what you want to do.
Beware: you are not editing the HTML file itself, but just its "in memory copy".
Javascript, as far as it is used as client-side technology, does not allow modifying files or in general accessing the file system. So if you're looking for some trick to write in the HTML, you're on the wrong way

How to create xml file with jquery

Is there a way to create xml file only with jquery/javascript ?
Not with browser JavaScript, no. You will need some kind of server to write to the file system for you. You could always build the file in JS and then send it through AJAX for the server to write though.
Use jQuery.parseXML to parse a trivial container string:
var xml = '<?xml version="1.0"?><root/>';
var doc = jQuery.parseXML(xml);
Then you can use normal jQuery DOM manipulation to append nodes to that XML document. Once you need to serialize the final document, you can use the answers to this question.
Your question is more complicated than it would first seem. Let's break it down into two parts:
Can I create a file using javascript?
Can I write XML to that file using jQuery/javascript?
Unfortunately, the answer to the first question is no. You cannot use javascript running in a browser to access the file system. This has security implications and will not be allowed by major browsers.
The answer to the first question makes the second one moot. You can create XML in javascript, but you'll have no place to write it.
If you provide more information about the reason you want to do this, it may be possible to find alternative solutions to your problem.
How about creating it by just using Javascript strings and then using this library?
http://plugins.jquery.com/project/createXMLDocument
You can create a document like this:
var doc = document.implementation.createDocument(namespace,rootnode,doctype);
doc.documentElement.appendChild(somenode);
And then serialize it to a string:
new XMLSerializer().serializeToString(doc);
But it will only be in memory. What else do you want to do with it?
Seeing as I'm learning XML myself, this is perhaps an illegal answer as it contains a possible question.
That said, I'm pretty sure that an XML document can be sent to the browser to display. That can then be explicitly saved by the end-user.

Importing external json files to a javascript script client-side without external libraries

I'm a bit new to javascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?
You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.
EDIT:
If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.
Yes there is. You can use the "document.write" to add scripts to the DOM at runtime:
in your case:
document.write('<script ...></script>');
Basically you are adding the script tag to the dom that will request the new file.
However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:
var x = { //json object };

Categories

Resources