I am still trying to get my head around how client server comms work with JSP/js and Struts.
I have a Struts property in my action that I can display on my page using
As I understand it this works by looking in my Action for a method called getMyMessage(). So this works ok for me.
I now want to be able to access this value from inside my Javascript so I can dynamically populate a test area when the page loads.
I use
$(document).ready(function( data )
{
$('#contextsTextArea').html(data.myMessage);
});
but this does not work. I know I could assign a var in my JSP that stores the struts property and reference this var in my JS but is there a cleaner way to do this?
JSP runs on the server, JavaScript runs in your browser.
You cannot use a JSP variable like 'myMessage' as a variable in your JavaScript. You can, however, turn it into text on the server so it works as JS when it gets to your browser.
So, try something like this:
$('#contextsTextArea').val('<s:property value="myMessage"/>')
When this JSP is processed on the server the JS code will be left as text while the tag will be turned into the text in your myMessage variable.
Note that you really need to JS escape myMessage otherwise, for example, if the message contains a single quote then the JS will break.
Have a look at your page source to understand what's going on here.
If you are talking about textarea you have to use .val() instead. Like the following.
$('#contextsTextArea').val(data.myMessage);
Related
Ello there,
I'm trying to assign the value of a javascript variable to a java variable. But I don't have clue how to do this? Say for example I have this:
<html>
<head>
<script type="text/javascript">
function return variable(){
var a = "hello";
return a;
}
</script>
</head>
<body>
<%
//The java code
String b = //how do I get that javascript variable here?
%>
</body>
</html>
Java script plays on browser where java code is server side thing so you can't simply do this.
What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server
HTML
<input type="hidden" id="hiddenField"/>
make sure this fields lays under <form>
Javascript
document.getElementById("hiddenField").value=yourCalculatedVariable;
on server you would get this as a part of request
You need to read something about a JSP's lifecycle. Try this: http://en.wikipedia.org/wiki/File:JSPLife.png
JavaScript runs on the client, but in order to change the jsp, you need access to the server. This can be done through Ajax(http://en.wikipedia.org/wiki/Ajax_%28programming%29).
Here are some Ajax-related links: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
The answer is You can't. Java (in your case JSP) is a server-side scripting language, which means that it is compiled and executed before all javascript code. You can assign javascript variables to JSP variables but not the other way around. If possible, you can have the variable appear in a QueryString or pass it via a form (through a hidden field), post it and extract the variable through JSP that way. But this would require resubmitting the page.
Hope this helps.
JavaScript is fired on client side and JSP is on server-side. So I can say that it is impossible.
I think there's no way to do that, unless you pass the value of the JavaScript var on the URL, but it's a ugly workaround.
you cant do it.. because jsp is compiled and converted into html server side whereas javascript is executed on client side.
you may set the value to a hidden html element and send to servlet in request just in case you want to use for further
As JavaScript is client side and JSP is Server side.
So Javascript does not execute until it gets to the browser,
But Java executes on the server.
So, Java does not know the value of the JavaScript variable.
However you assign value of Java variable to JavaScript variable.
I have a page. Which contain json method to load data. I call this method on page load. It works properly. The problem is when I view source of that page I don't see the generated code.
My concern is the search engine will never see the content even if end user see it.
Is there anyway to add it? If so how it can be done?
Here is the example of code I use
$(function(){
//Call to the server to get data.
var content = "Some data"; //from the json call
$("#content").html(content);
});
});
Most if not all search engines will not recognize content inserted into a page from Ajax/Javascript, this is why you need to load this content with in the page if you want the search engine to recognize it.
Your question seems to me a copy of what's asked over here and in Is there anyway of making json data readable by a Google spider?
So, Progressive enhancement and Unobtrusive JavaScript are the way out..
In addition to these, optionally, but importantly, test the crawlability of your app: see what the crawler sees with "Fetch as Googlebot".
I want to insert the following include tag into my webpage using JavaScript. <!--#include virtual='includes/myIncludeFile.htm' -->
I have tried the following but it doesn't work: jQuery("<!--#include virtual='includes/myIncludeFile.htm' -->").appendTo(jQuery("body"));
I have outputted jQuery("<!--#include virtual='includes/myIncludeFile.htm' -->") to the console and it thinks that it is a comment object (see screenshot).
Where am I going wrong and how can it be done?
The basic premise of what you're trying to do isn't going to work. The HTML "include tags" you're using are also known as "server-side includes." That is, they are processed on the server before the page is sent to the client.
By the time the JavaScript code is executing on the client, the server is already processing the response. The client-side code can't initiate a server-side include.
One thing you can do from the client-side is use something like jQuery's .load() function to make a request to the server and load the response into a specified element on the page. Something like this:
$('#includeDiv').load('includes/myIncludeFile.htm');
This would dynamically load all of the contents from myIncludeFile.htm into an element of id "includeDiv" on the page.
Just explaining my question in short.
I have asp.net website with root structure as following
root Directory->
Admin
abc.aspx
xyz.aspx
index.aspx
Now I want to redirect from abc.aspx to index.aspx.
I'm using JavaScript as
window.location = "../index.aspx";
but found no any luck.
This is a very odd trick. But it works.
Have a hidden field in the page. This needn't be a server control. (Or if you want, you can even do without one.). For brevity sake I am assuming you've used a server HiddenField control, called hfNavUrl. Do something like this.
hfNavUrl.Value = Me.ResolveUrl("~/index.aspx")
Once this renders you get the full url. Find the hidden field value in javascript and work your javascript code:
window.location.href = document.getElementByValue('hfNavUrl').value;
Update: Doing it w/o using controls
There are two ways about this. However, you'd have to embed the code in your page markup file (i.e. the *.aspx file). The javascript way is something like holding the url into a variable.
var url = '<%= Me.ResolveUrl("~/index.aspx") %>';
You can now make use of the value inside of this variable at a later point of time.
Alternatively, you can simply have a <a> element with its href set directly:
Home
Ps: Me.whatever is the vb.net way of doing things. Replace it with this if you are working with c#
Try using "window.location.replace('/index.aspx')"
You issue will be resolved by one of the below solutions from java script by providing the type of application.
If Application created as Website
windows.location='/index.aspx';
and
If Application created as Virtual directory
windows.location='/[Virtual Directory Name of Root Folder]/index.aspx';
OK let's say I have a jQuery function... it's an AJAX Post... well after success I do this
$("#title"+id).html(Z);
and Z is var Z=$("#"+X+" textarea.editAnswer").val();.
However if I type in the textarea something like <script>alert('Test');</script> and then press save... it will save it and the test alert will popup. So... how do I fix this?
This is code injection and should be avoided with HTML encoding.
Either:
return HTML encoded result from the server (after you've saved it) and populate that into your #title element or
use escape() Javascript function to encode it on the client and populate that result into your #title element
Depending on the scenario, you should always use the one that makes it harder for the user to hack. I'd suggest you use server side approach, because users won't be able to override your custom javascript function and address their own twisted agenda...
Your options are escaping the code or using whitelists.
Do you need the html code? Take a look at this question: How to prevent Javascript injection attacks within user-generated HTML
And then these ones:
Script injection - form validation (jquery)
Do you only run htmlspecialchars() on output or is there other functionality you also do?