I am working with JSP pages and I need to .append() into a DIV element some java codes.
$("#myDiv").append("<% out.println("ali"); %>");
The previous code is actually wrong because there's quotes, so I escaped them.
$("#myDiv").append("<% out.println(\"ali\"); %>");
But I wasn't successful, nothing was appended to #myDiv
This is correct way to append. If nothing is appended, then the div might not exists in the page.
Try using firebug and check what6 is the output of console.log($('#muDiv'))
Your code should work, it sounds that jQuery is not loaded correctly or there is another problem.
http://jsfiddle.net/5PTwN/1/
Try this
$("#myDiv").append('<% out.print("ali"); %>');
below line also should work because java code will execute server side and inside append will be replaced with "ali".
$("#myDiv").append("<% out.print("ali"); %>");
Related
I am having issues printing my HTML code, extracted from an external URL.
This is what I did to get the HTML code:
Document document = Jsoup.connect('url').get();
and this is how I am trying to print the document:
response.getWriter().print(document);
The thing is that the last line of code opens the code as if I were executing it, showing it on a "browser" rather than just showing me the HTML code. How can I fix this so I can see the HTML code?
I just had to add
response.setContentType("text/plain;charset=UTF-8");
before calling the getWriter().
I have tried the following code to get the google translator text using selenium:
result=driver.findElement(By.xpath("//body/div[#id='result_box']/span"));
I also tried these:
1.result=driver.findElement(By.xpath(".//body/div[#id='result_box']/span"));
2.result=driver.findElement(By.xpath("./*div[#id='result_box']/span"));
3.result=driver.findElement(By.xpath(".//div[#id='result_box']/span"));
4.result=driver.findElement(By.xpath("//div[#id='result_box']/span"));
5.result=driver.findElement(By.xpath(".//body/div[#id='result_box']/span"));
6.result=driver.findElement(By.xpath("./*[#id='result_box']/span"));
But none of the above works. I then tried to get the text by:
result=driver.findElement(By.id("result_box")).findElement(By.tagName("span"));
translatedtext=result.getText();
This returns a result but when I try to show the result in JTextarea it shows me '????' instead of the actual translated text.
I have also tried 'result.getAttribute("innerHTML")' but it also shows some question marks (?????) instead of the original translated text in JTextarea.
How can I solve this problem?
The result box has tag <span>, not <div>
result = driver.findElement(By.xpath(".//span[#id='result_box']/span"));
Or
result = driver.findElement(By.xpath(".//*[#id='result_box']/span"));
With double slash.
you also can use css selector like this:
result = driver.findElement(By.cssSelector("#result_box>span"));
people say that is faster than xpath
This worked for me, however i used python,you can try using equivalent of find_element_by_id function in Java
driver.find_element_by_id("gt-res-dir-ctr").text
I need to return an html code from a JAX-RS endpoint. I followed some tutorials and got to know that I can return it as a String. But the problem is I need to add JavaScript function to that returning HTML. How can I do it?
My code snippet.
return "<html><body>Hello World</html></body>"
Here you can add javascript function inside the returning String. You can follow the same steps following when declaring a javascript inside a html page.
I will provide an example:
return "place your script here Hello World"
Make sure to put the script inside tags.
I'm using struts2 framework(java/js/html/css combo) for my webapp. I am reading a text file from server and I want to write the response to an iFrame present in the same jsp.
Flow:
(1) On click of a link, I pass the relative URL of the text file to jsp.
(2) When the jsp page loads, the java code in the jsp reads the file from server.
(3) Now this response has to be written to an iFrame present in the same jsp file
Can anyone plz help me in writing such response to an iFrame?
Thanks in advance :)
[code not tested, only a demostration of the concept]
here's some very rough idea as to how to fix your code, they definitly not the best but they should be enough to help you understand the concept.
However I'd still recommend going over the whole concept and maybe come up with a more efficent way to do what you need.
if you insist on using iframe, you need to make use of 2 seperate jsp as W3C says in "Implementing HTML Frames":
Any frame that attempts to assign as its SRC a URL used by any of its ancestors is treated as if it has no SRC URL at all (basically a blank frame).
so you'll need 2 jsp, the first one is basically what you have but the the src of the iframe changed to:
<iframe scrolling="yes" width="80%" height="200" src="second.jsp?content=<%=all%>" name="imgbox" id="imgbox">
and the second one will be something like :
<html><body><%= request.getAttribute("content") %></body></html>
From the code you've shown you forced a "content update" on the iframe by using javascript. The proper/usual way to update an iframe is to provide different input parameter to the second jsp and let it update it for you.
Finally, I'd recommend using JSTL as much as possible instead of scriptlets. It is much cleaner.
What you need to do is set the src attribute of the IFRAME to the jsp url when your link is clicked. Another way to do it is doing something like this:
<iframe src="" name="iframe_a"></iframe>
<p>W3Schools.com</p>
with the correct parameters of course
I am relatively new to working with JSPs and I have a feeling I'm overlooking something simple. I have a segment that appends a key onto a URL before sending the user back to where they came from. The key is a string value and when it consists of only numberic values(for example 12345) it works fine, but when it contains non-numerics(for example abcde) it simply appends "#" to the url and stays on the same page.
<core:when test="${dataTransferObject.someBoolean}">
Back to Home
</core:when>
When it's a string the JavaScript will be illegal–it will think you're trying to reference a non-existent JavaScript variable. You will see an error your JavaScript console.
Don't do any JavaScript operations; JSP is evaluated on the server side before the client sees it:
onclick="javascript:location='path/back/to/their/home.request?cachekey=<core:out value="${dataTransferObject.stringVariable}"/>';return false;"
Better yet, use JSP EL:
onclick="javascript:location='path/back/to/their/home.request?cachekey=${dataTransferObject.stringVariable}';return false;"
Also, if this is the JSTL core tag library, the canonical prefix is "c".