What I'm trying to do is what can be done in a browser by running this javascript: document.getElementById('foo').bar().
I don't know how to go about doing this in python. I used to do stuff with submitting forms using twill and sending responses with urllib2, but I never had to do any javascript. Where should I start?
I guess you are building up your html/js code in the server side and when requested, you will serve that code to the client? As client code will be served you can add it to you logic in the server side:
getFooElement = 1;
if getFooElement:
print "document.getElementById('foo').bar()"
else
print "HTML/JS code"
Related
I have to call a javascript function from my Model or Controller. I can call it from the view file but how can I call a JS function directly from Model.
Is there a way to do that?
PHP is a server side language. This means the PHP file will get processed on the HTTP server and then deliver the generated HTML page to the client.
Javascript (JS) is interpreted on the the client (browser) and interfaces with the HTML and other remote resources.
Due to this model, PHP and JS know nothing about each other. So to call JS directly from the PHP file is not possible.
If you are looking to make a client browser call a JS function and the PHP script defines the values for the JS function you can use echo() and some string concatenation
//this is an example and will not run.
echo "<script>myJsFunction('" . $arg1 . "','" . $arg2 . "')</script>";
This will embed the JS function call in the HTML page returned to the client. Once the client renders the HTML page it will execute the function call as it comes across it.
If you could give an example of what you trying to do I might be able to give you a more accurate answer.
For a deeper explanation of the client server architecture and where things get executed have a look at Web Browser Client Server Architecture
No, try understanding the MVC pattern, the idea is that the model is not aware of the view and doesn't have to be. Also you can't call JS with php, which is a server side language and doesn't know anything about the client side scripts.
What you want is to implement long polling. I recommend you to learn and read about the basic technologies involved before trying to implement something.
http://en.wikipedia.org/wiki/Push_technology
http://en.wikipedia.org/wiki/Comet_%28programming%29
How can I pass a javaScript variable into ruby. I want to do something like this but I don't know how to express it.
function save(){
var g = document.getElementById("self").value;
<% #owner.info = g %>
}
Another possible work around is that i would need to be able to extract contents of a text area through rails and not javascript.
Can anyone help me?
What you are attempting to do doesn't make sense with a vanilla rails installation and javascript. Here's a good workflow that accomplishes what you're trying to do along with some details:
1. A page is requested from the server
The ruby code that runs rails and your application is executed on the server. The server receives a request, executes the ruby code, and sends the response as an html document.
2. A user gets the response from the server
The user's browser receives the html and turns it into a pretty web page. It's at this point that any javascript related to your application is executed in the user's browser. The connection with the server has been severed and no further ruby code will be executed until another request is made.
3. The user fills out an ajax form
On the page rendered in step 2, you have a form. Following this guide you can tell this form to submit via ajax. That means instead of requesting a new web page, the browser will send a special request using javascript to the server. The server can save the form values to your database and send a response back to the browser. All the while the user hasn't left the page they are currently viewing.
Alternatively you can skip the ajax and have the user submit the form, but you'll need to redirect them back to the page they were viewing (and probably adding a note the form they submitted was saved).
this is what I want to do. I am a newbie at javascript and django. Please let me know how I can go about doing this:
Client clicks button on web page, javascript downloads html content from external website.
Javascript sends html content (likely a web page) to django server.
Django server executes a specific function on the html content, and returns
a JSON serialized piece of data and
a modified version of the downloaded html content.
Javascript displays 1 and 2 client side.
How do I go about doing this? I suspect I need JQuery AJAX for 1), but I can't seem to get it to work with external html. For 2)-4) I am completely lost. Please help, forgive my newbie-ness.
UPDATE: I don't want to get the html content from the server for a reason. If I do that with a lot of users the site I get content from will block the server from further downloads.
Why not make that all bit shorter:
1) Visitor clicks on button on webpage and your webpage sends address of html page to your django server.
2) Django executes the web request using urrlib2 (for example) and downloads the webpage by itself.
3) Django executes that function on content and returns the a) and b)
4) javascript displays a and b.
So what you need:
1) jQuery ajax to send the url to your server.
2) look up some basic examples for django urllib2 about hot to make web requests. Its pretty simple and easy too.
3) Get http://www.crummy.com/software/BeautifulSoup/bs4/doc/ for parsing html you have downloaded with urllib2. Modifying and parsing html documents is super easy with it. If you look up examples and try them out, you will see it yourself.
4) And then just return the html and/or json using django's own Http methods.
Alan
I have something like the below code. I need to pass the variable selectedIndex to the JSTL code. How can I do this?
function updateSP(selectedIndex)
{
<c:if test="${entry.key eq IC.oList[selectedIndex]}">
}
First, you need the following concept right: Java/JSP runs at the server machine and produces a HTML/CSS/JS page. The server machine sends HTML/CSS/JS page over network (HTTP) to the client machine. Client machine retrieves HTML/CSS/JS and starts to interpret HTML to display a markup structure, apply CSS to style and position the structure and execute JS on the resulting the HTML/CSS.
There is no means of any line of Java/JSP code at the client machine. Rightclick page and view source. The only way to pass Java/JSP variables to Javascript is to just output them as if it's a Javascript variable so that it has instant access to it once it runs at the client machine. The only way to pass Javascript variables to Java/JSP is to just send a HTTP request with that variable as parameter and have Java/JSP to listen on that specific request.
More background information and code examples can be found in this article.
jstl is executed in server side so you can't pass a javascript variable to jstl.
What you can do is generate dynamic javascript using jstl.
Please use the tool to format your code.
Did not have luck with these examples:
Javascript File remove
Javascript FSO DeleteFile Method
Deleting a File
There are no special permissions on the file.
Is there a way to do this in JQuery?
The requirement is - a certain file must be deleted from the web directory when another page is loaded. There is no security issue as this is on a closed network.
Any help is appreciated.
Thanks.
With pure JavaScript, it can't be done. Using an AJAX call to a server side script that deletes the file would work though.
Javascript cannot delete files, it is prevented as it would lead to HUGE security vulnerabilities. THose links are for ActiveX controls that are handled through JS. Use a server side language.
You can't delete files over HTTP (well in theory you can, but it's not implemented.)
The easiest way is to set up a tiny server side script (e.g. in ASP or PHP) and to call that from JavaScript. The server side script needs the proper permissions to do the deletion, but otherwise there is no problem.
In PHP the start would look like this: (Not expanding solution to a fully secure one because you're not saying what platform you are on)
<?
// STILL INSECURE!!!!
// Do not use in any public place without authentication.
// Allows deletion of any file within /my/files
// Usage: filename.php?file=filename
$basedir = "/my/files";
$file_to_delete = $_REQUEST["file"];
$path = realpath($basedir."/".$file_to_delete);
if (substr($path, 0, strlen($basedir)) != $basedir)
die ("Access denied");
unlink($path);
?>
you would call the script like this:
http://yourserver/directory/delete_file.php?file=directory/filename
You cannot delete a file on a remote server using only JavaScript running in a visitor's browser. This must be done with a server-side script.
If you are doing this in a RESTFUL way, you would send an HTTP DELETE request.
jQuery's ajax method states that you can use the method parameter to specify 'DELETE' but notes that some browsers may not support it.
Obviously you will need a webserver which will accept a DELETE request, and apply some sort of authentication/authorization so that joe random visitor can't delete your files. I believe Apache's mod_dav will get you started here.
Javascript is a client side language. So you are not able to delete file on server directly. All examples that you provide may be used only for deleting files on your local machine but not into server.
But you may call some server page function that will delete file.
You can't delete files with JavaScript as it is run locally. So, it doesn't even touch external files.
You need to use a server side language that has access to editing the files such as PHP, RoR, or ASP.
You can however use jQuery to call the server side code via AJAX such as $.get or $.post and then the server side code deletes it and it would seem as though JS is deleting the files.