Locals in Javascript/JQuery like Locals in Rails? - javascript

I create a Js Application and want to have English and German locals, which i can switch via button.
Is there a way to insert locals from a extra file in a .js file, like the function t'...' in Rails ?

As far as i know there is no way to do it directly and the reason is fairly simple too, erb is executed at the server side and javascript is a client side language which means its executed in your local browser, thats why if you even try to pass a variable between the two you'll have to make a request to the server, However this problem is tackled by calling an AJAX request, this AJAX request does the same thing as sending a new request to the server however it does that without refreshing or reloading the page to it gives the users the illusion that no request was made.
a guy asks a similar question here:
http://www.quora.com/Ruby-on-Rails/Can-I-pass-a-JavaScript-variable-to-a-Rails-method
and you can learn more about AJAX here:
http://www.w3schools.com/ajax/default.asp

Related

Questions about "response.redirect" on Kitura

Hi guys,
I'm not familiar with web server, client and AJAX. I encountered redirect problems on Kitura.
The delete route can redirect to "/api/v1/users/list" succeeded.(I saw a message through print function)
but the browser doesn't reload data(refresh) for /api/v1/users/list.
Please following code, Thanks!
Q1-0)Do I need to perform a manual refresh for browser?
Q1-1)If I should, which side is better for that? (server side or browser side)
Q2)Do I need to do refresh action by manual, when I using AJAX delete method?
Server side method "delete"
---------------------------
...
router.delete("/api/v1/users/delete/:id" ....
_ = try? response.redirect("/api/v1/users/list", status: .seeOther)
...
Server side method "get"
------------------------
...
//list all users.
//each user have a delete button that performs AJAX delete method to "/api/v1/users/delete/:id".
router.get("/api/v1/users/list", ...
print("get /api/v1/users/list")
...
Short answers:
Q1-0: In your case, yes.
Q1-1: In your case, browser.
Q2: In your case, yes.
Longer answer:
This really depends on the architecture of your app:
Client/server: You build an API that sends/receives JSON or XML through REST endpoints. On top of that, you build a JavaScript client that uses AJAX to communicate with this API. This is what you seem to be doing. However, your AJAX requests should only send/receive JSON or XML data. Any page updating, reloading or redirecting should happen client-side.
Server-side: Here, most of the logic happens on the server. You use HTTP GET and POST to request pages and submit forms. The server then processes these requests and returns an HTML page for the browser to render. See https://github.com/svanimpe/swift-blog for an example that uses Kitura and Stencil.
Client/server is more flexible as you can build several clients (web as well as native apps) for the same API, but is also more complex, as it's a distributed architecture and usually involves multiple programming languages and some code duplication.
Server-side apps are generally easier to build for beginners as they are monolithic and involve very little non-Swift code (in your case).

ajax - Ajax vs document.getElementById().innerHTML

I am new to Ajax and web development in general. When I googled Ajax, a lot of sites (like here) said that one of the key features or Ajax is that you can dynamically update content on the webpage without reloading it.
My question is this: can't you just do this by using document.getElementById("...").innerHTML = "whatever you want it to change to"? I know that with Ajax you can make requests to a webserver and whatnot. That is not my question. My question is that why do people claim that changing a webpage without reloading it is something special about Ajax when you can do it with normal JavaScript?
And also, in the link above, it said that with Ajax you can "request/receive data from a server - after the page has loaded". Why "after the page has loaded"? Is there another way to request/recieve data from a server while the page is still loading?
Thank you!
My question is this: can't you just do this by using
document.getElementById("...").innerHTML = "whatever you want it to
change to"?
You can indeed change the inner markup of dom Element instances using this property.
I know that with Ajax you can make requests to a webserver and
whatnot. That is not my question. My question is that why do people
claim that changing a webpage without reloading it is something
special about Ajax when you can do it with normal JavaScript?
Javascript is client side. Ajax is special in that it requests data from a server so you can use it in the client (javascript).
Javascript by itself (understand, without the XmlHttpRequest object) does not allow that. All you can do is client side dom manipulation, not knowing what's on the server side (which means, among other things, no access to shared databases)
And also, in the link above, it said that with Ajax you can
"request/receive data from a server - after the page has loaded". Why
"after the page has loaded"? Is there another way to request/recieve
data from a server while the page is still loading?
Yes.
jsp, php, are two examples of server side languages. When you request http://page.php (for example), the server routes the request to the *.php interpreter. The code inside the page is then used to generate http headers and html content back to client. This is a round trip that happens every time a page is accessed. The page is first loaded using this system.
Ajax allows you to proceed with subsequent calls to any php script, while the page is already loaded.
ajax is a way of loading data from the server without reloading the entire page, innerHTML is one way of injecting that data into the page...so ajax is a way of communicating with the server while, innerHTML is a way of manupilating the page.
Hello here is the key deference between ajax and document.getElementById().innerHTML is
AJAX
AJAX will load the content when you want to change for the perticuler div and change the content the content are not stored in any where in current web page
document.getElementById().innerHTML
Where when you want to change the content without ajax then you need to store the content in any javascript variable or in hiddent html so it will load the content if you want to show or not.

ASP.NET call a Javascript function

I'm working on a project that uses IP Payments to process transactions. The project involves a web form written in ASP with Code-Behind written in C#.
IPP offers an iFrame implementation, where you can put an iFrame in your page and display a small IPP page with fields for entering credit card information. The idea behind this is that the credit card info will only be handled by IPP and never by the server running the page, thus there is no requirement to ensure that card data is kept secure.
In order to display the IPP page in the iFrame though, a session needs to be initiated with IPP. The server initiates the session, and passes in a SessionID variable. Upon a successful session initiation, a Secure Session Token is returned to the server. The server then needs to "force" the client's browser to GET or POST the SessionID and the SST (Secure Session Token) to the IPP website. This is where my problem is.
I wrote a Javascript function in the ASPX page that would accept two parameters - the SessionID and SST - and send them to the IPP website. I'm now trying to call this Javascript function from my C# code upon successful initiation of the IPP session. However, I have been completely unable to do so.
I've done a lot of searching, and the one answer I keep coming across is to use either RegisterStartupScript or RegisterClientScriptBlock. The problem is, these seem to insert text directly into the page, rather than calling an existing function. Assuming I inserted my function into the page via one of those functions rather than writing it into the page myself, it still doesn't solve my problem of how to call said function.
Now it is possible that I'm going about this the wrong way, and there's a much better way to get the client's browser to GET/POST the SessionID and SST; if so, please tell me. I'm inexperienced with web programming and am thus learning as I go and making up solutions along the way that are quite likely not ideal.
Thanks in advance.
I think this should work:
Lets say you have something like this in your HTML:
<html>
<head>
<script>
function sendValuesToIPP(sessionId, sst){
//do stuff
}
</script>
</head>
</html>
If you do this in your C# code it should work
ClientScriptManager.RegisterStartupScript(
this.Type,
"some_key_you_want_to_identify_it",
string.Format("sendValuesToIPP('{0}','{1}')", SessionID, SST),
true);
Keep in mind that I'm assuming you have SessionID and SST properties server side, you can get them from wherever you want and just add them to the string that will actually call the function when registered in your ASPX.

Using JavaScript to send String to Servlet, and Results from servlet back to JavaScript

First of all: sorry for my bad grammer. English isn't my native language, but i will try to exlpain my problem as simple as i can.
I'm working on a web-application, where user can enter a link. (Question 1) This link should be send to the server/servlet and will be progressed to other things. (Question 2) After the progression, the servlet will send a json-array (?) back to the javascript-part of my app.
I'm completly new to this kind of stuff, but its very important to me, to find out how this works or better, how i can make this work. Its actually very simple, but i used plenty of weeks and cant figure it out.
The application is using the SAP UI5-libs (Question 3), where i would also like to know, if there is any possible way, to parse JSON with the UI5 libs.
I hope, i could explain my problem good enough, so i can get some help. Thanks to all!
The 'sending' of the string to the server/servlet would happen via ajax in either POST or GET form. That is up to you.
I recommend you use a javascript plugin like jQuery (JQuery Ajax API) because the regular ajax code is a bit messy.
As for the servlet/server communicating back to the client is as simple as writing to the page. In a typical servlet context it would be something like
out.print("This is a message");
where Ajax automatically returns the content of the entire page upon callback.
So in conclusion:
Consider test.jsp your servlet. I wish to send "Hi" from the client (being the browser) via GET to the servlet and I want the servlet to say "Hello" back.
I would open an ajax request of type GET to the url "test.jsp?param=Hi". In the servlet I receive this page request and process it. The servlet discards the parameter because it is not used and outputs "Hello" to the page.
In the client the ajax will have returned "Hello" and I can use this to put it into a var or whatever and all of this happened while not refreshing and not navigating in the original document where I did the javascript.
Another way is using websockets where you basically use sockets in javascript to send and receive any kind of data.
Also please check out this possible duplicate question: How to send a string to a servlet from javascript using xmlhttprequest

How to pass javascript variables to rails variables

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).

Categories