I'm using pyfacebook on the backend and javascript on the client side. Now if I want to pass a variable to the javascript from pyfacebook. how would I go about doing that, any ideas?
You can't pass a variable directly, as JavaScript is running on the client (browser), and Python is running on the server.
You could make a XHR (AJAX) request from JavaScript to the server which would then return your values back to JS (JSON could be used here).
Or you could put a hidden field to your markup that would have the value in it's "value" attribute. You could then read that with JavaScript.
ps: your question really isn't related to pyfacebook but Python (or any other server side technology) in general and that has been covered here many many times.
Related
So I have a web page, and I would like to programaticly create a text file (lets say it has the words 'hello, I am a text file' in it) on a new directory on my website. The program will be in another directory on the website.
e.g.
https://www.example.com/txtbuild.html is trying to programaticly make https://www.example.com/texts/hi.txt
Is there a way to do this with HTML/Javascript?
EDIT:
I am on Github
You can't do it with HTML/Javascript alone, you need a functional language on the backend (nodejs, php, python)
You can use ActiveXObject, but it won't work in all browsers.
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
https://msdn.microsoft.com/en-us/library/5t9b5c0c(v=vs.84).aspx
If, when you say "JavaScript", you're referring to a node.js application running on a server, then this is possible. It doesn't have to be node though; it could be a Django site, or an ASP.Net site, doesn't matter. You can't have JS code in the browser create files on your server... the JS in the browser is executing on a client machine, and doesn't have access to the server's file system.
You could create an endpoint to which your clients could send requests that would initiate the creation of the file.
You could also allow clients to PUT or POST files to your server, but again, this is something you control from the server side of the application. Your webpage (i.e., HTML file as you put it) cannot create files on the server itself. Your server allows clients to send it files in a specific manner, and the client must adhere to those rules.
The short answer to your question is no.
The long answer is that you have the following alternatives:
Use a form on the Browser end, send the data back to the server, and then use a server-side language such as PHP to receive and save the data. No JavaScript required, but you do need server-side programming.
You can make the process more immediate by using JavaScript on the browser end to send data back to the server. This is called Ajax. You will still need server side processing, though.
Note that it is probably a very bad idea to simple accept user data and save it directly. There are two things you should consider in your development:
Always filter the incoming data against the possibility of receiving and accepting carefully crafted malicious data.
Consider storing the data in a database. Apart from being easier to manage (you don’t have to worry about filenames, for example), they can do less damage there.
You can achieve this in IE browser using the following code.
<SCRIPT LANGUAGE="JavaScript">
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("C:\test.txt", True);
s.writeline("HI");
s.writeline("Bye");
s.writeline("-----------------------------");
s.Close();
}
if you are looking for a goos reliable solution then better to use PHP and other server scripts.
Using PHP, HTML and Javascript.
With PHP we are using a MVC Framework.
I have a fundamental question regarding web communication, that perhaps someone could clear some things here.
REST apart, and taking only "post" and "get":
Let's say that, if we do:
http://blabla.com/companyA/income/
It will list all income of a given company.
If we do:
http://blabla.com/companyA/income/2010/
It will list all income on 2010 for that company.
And so on.
Now, I wish to allow the user to, from a html form, select some values, and according to those values, return, from the server, the appropriate data.
How does this work?
a) Should we concatenate the URL string on the client side, (form action) and send it to the server side?
b) Our, should we send the params to the server side, and it returns the URL?
Anyway will work? Only one way will work? What are the consequences of those paths? Is there a third possibility?
a) Should we concatenate the URL string on the client side, (form action) and send it to the server side?
That's how it's always been done historically and it still works fine. It's the equivalent of constructing the complete HTML by hand, but using PHP (or any server side language) to help you remove manual labor, by basing the HTML on the data you have easy access to.
b) Our, should we send the params to the server side, and it returns the URL?
That's unnecessary, you already have all the data ready so going with a) is more solid since it lets the client become a bit dumber, by excluding the render-url-from-arguments logic.
Is there a third possibility?
Yes. Since you already expose lists of resources at /companyA/income/2010/ (perhaps through JSON), you could serve an empty page from the server side, trigger an ajax call to your backend and generating the list dynamically on the client side. This brings a bunch of things to think of, some are:
How is SEO affected when (google) bots request empty pages?
Can we reuse the rendering logic for different views, without reloading the empty page?
You could read up on SPA, or, Single-page applications.
You should make url when processing with PHP, so you can pass ID or any other information from server. Making URL from client side requires additional processing.
<form action="companyA/income/<?php echo (int)$years; ?>">
...
<form action="<?php echo $this->makeUrl('companyA/income', $years); ?>">
...
Many places have said that only elements with a name attribute go to the server on page change, and only the element name and its value attribute's value travel between client & server. Is this a PHP feature or is it also present in other scripting languages? For example, is this the case with Node.js, or any of its popular server frameworks like express or grunt? Also, are there ways to send other elements or attributes to the server?
I know that AJAX can cause pretty much anything to go to the server, but this is usually asynchronous, and even when it isn't the info doesn't usually go to the server right when the page is sent. If you have any relevant info on AJAX, though, please share it.
When you give an element a name attribute, the browser will send the form data in the body of the request (if using POST) or in the query string (if using GET). This happens no matter what language or framework you use.
name is the only attribute that does this - it will not work with an id - but you can also do this with AJAX, by passing a querystring to XMLHttpRequest.send (if you're using jQuery, read up on jQuery.post). Requests via AJAX are, to the server, identical to requests from the client. If you send data in an AJAX POST request, it will be identical to a request via an equivalent form to the server. This is the same whether you're using PHP, Node.js, or any other web framework.
Helpful references:
http://en.wikipedia.org/wiki/POST_(HTTP)
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data
I am new here and I have a question on javascript. Do javascript has function where it can automatic send an alert/notification email to recipient within a given period? For example, in the database, there is a "date" attribute. I want a javascript function to send an email automatically after 3 days starting from the "date" in the database..Thanks in advanced
Unfortunately, JavaScript (the way you are experiencing it) is a client-side language and cannot do what you want by itself. You will have to "ask" a server-side language to do that for you (note that JavaScript has a framework built on top of it that runs server-side, thanks #Cuberto)
Basically, you can do the scheduling in two ways:
1) Perform a countdown in the client browser, and when the timer hits 0 initiate a mail-sending request towards the server (very impractical, and sometimes outright impossible, as in your 3-day example).
2) Initiate a request immediately, and use cron or equivalent Windows service to do the scheduling for you (the "right", and much preferred, way).
You can't send an email directly with javascript.
You can use php or asp or something else that have server functionality.
For sending emails JavaScript is not the right language. JavaScript is executed on the client side from where you cannot send emails (unless you use node.js). It also doesn't have access to the database which is on the server side.
What you are looking for is probably a PHP script to send emails. From php it is quite simple, just use the mail-function (http://www.php.net/manual/de/function.mail.php).
For sending automatically after a period, you need to implement some logic. I'd implement a script that is executed regularily that checks whether it is time to send the email and after it has been sent, it will be marked as sent in the database, such that is it not send again.
I'm working with a .js client and have and object that I need to write out to a file on the server. A couple of questions - file i/o with JavaScript is new to me... I was planning on using jquery and json. I'm using java serverside. I don't have a problem reading what I get back from my servlet, but the file i/o is killing me! A couple of questions:
I can open a file I generated myself via the .js with an $.ajax call, but it's not handling my json syntax (I tried both an $.getJson and $.ajax - handwritten json, so I might (probably) are doing something wrong with it). I used firebug's console and it looks ok...
How can I write my object to a file on the server?
Then, when I want to read it, what do I need to do to process it? Right now I'm using a jsonFilter function (uses JSON.parse if that's available, otherwise eval) to process data that I'm getting from the servlet.
The object I'm writing isn't simple, but it's not super complex either. There's an array that contains an array, but that shouldn't make a difference if the software is both reading/writing it.
Thanks for any help! I'm at a loss - tried alot of different things.
You can open a file located on the server via ajax by querying the file and loading it into a JSON object. You might want to LINT your JSON
You can not write to an object on the server via the client. This is a severe security breach.
Common practice is to change the JSON data and then send it via ajax to server-side code. The server will then do the file IO.
Yes using JSON.parse otherwise eval is indeed correct. I would recommend json2.js
The data should be fine as long as it passes JSONLint.
Your main issue is that it's impossible to write to the server from the client. Get the client to load the data through ajax change it and then query the server to update the file.
js don't have i/o property;
you should use ajax or http request to send message to server,and tell server to do de i/o action...