How to catch an iframe redirecting? - java

I have a WebView with an iframe inside. The contents of the iframe do a redirect to another URL that doesn't allow embedding in an iframe. Not a problem, actually, I just want to start the external browser activity in this case instead. The problem is that I can't find a way to intercept the redirection. None of shouldOverrideUrlLoading(), onPageStarted() or onLoadResource() get called for the redirect, hence I get no chance to start the activity.

In the html change the iframe tag to call some javascript function when a redirect happens
example: iFrame src change event detection?

For anyone else with this problem, we found an even more simple solution to this than calling a javascript function. Simply add "target= _top" to the iframe redirect (see http://www.w3schools.com/tags/att_a_target.asp). Then shouldOverrideUrlLoading() will be called in the Android code.

Related

Call HTML page with javascript content

I am stuck at an issue, I don't know if its possible or not.
I need to call a HTML page, but the page has some content being loaded via Javascript too and I need to get that also.
Is this some how possible ?
Currently I have used plain: new URL(url).openConnection() and it's not returning that.
Any Help ?
Thanks
To do that you'll need to host a WebView in your application. It can be hidden if you want. Call the webview loadUrl to load the content you want. Handle the WebViewClient onPageFinished so you know all of the content has loaded, and then use the javascript bridge interface (addJavascriptInterface) to pass the HTML back up to the application.

How to map server response retrieved in jsp to an iFrame

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

With the click of an HTML button, how to INVOKE a java method AND REDIRECT to another HTML page?

I have a button through which i INVOKE a java method :
<h:commandButton value="My Schedule" action="#{empDutySchedBean.viewMyTask}" rendered="#{welcomeBean.workSchedule}" class="mainLinks"/>
now through this method viewMyTask() i get a List of values returned from the database.
public List viewMyTask()
{
setEmpID((String) session.getAttribute("userName"));
setEmpDuty(ts.getMyTask(getEmpID()));
return empDuty;
}
From here i want tht with the click of a button, the user is redirected to another page and the List data shows up.
I dont understand how to REDIRECT to another HTML page and display the LIST data.
NOTE: --> this button is basically a menu on my webpage. For the other menus i have used an anchor tag. However as you cannot invoke a method using anchor tag, i have used button tag. But because of this, I am not being able to redirect
Some Ways that i think it can be solved are:
use anchor tag instead on button to redirect to the other page AND then use onLoad method of javascript to INVOKE java method
invoke another method after viewMyTask() to perform the redirection.
I dont kw how these methods would work though.
First off, I assume you already know how to return the data Java side, in which case this is really just a javascript question. To do simple post-gets, I usually use Ajax, like so:
new Ajax.Request(myUrlToCall, {
method:'POST',
onComplete: function(transport) {
window.location.replace(theRedirectUrl);
}
});
This will call whatever url you wanted to call Java side, then on the completion of this call, will redirect you to whatever url you want to go to. If you need to send some parameters to the java side, all you need to add is:
parameters:myParamVar,
after the method:'POST' line, and if you want your java side to return the url, then all you'd need to do is send it as json or similar in the response, then do:
window.location.replace(transport.responseJSON["theRedirectUrl"])
Hope that helped!

Access Javascript from Java

I m trying to access JavaScript function from Servlet code. But I'm getting the error shown below.
Here is the code:
out.println("<FRAME src=\"javascript:parent.newWindow('" + URL+ "') \" scrolling=No noresize />");
And this is the error that occurs in JavaScript:
Object does not support this property or method;
You can't access a Javascript function from your servlet code. Javascript executes on the client (= your user's browser) and the servlet code executes on your server (for example Tomcat, JBoss, whatever you're using).
What are you trying to accomplish with your code? I'm sure there's a simpler way to do it than what you just described.
[edited]
I see you just updated your description, so here's my view:
I'm guessing that you want to display a page to the user and when the page is displayed, you want to open a new window which will display another page using the URL parameter to point its address. If this is the case, you should probably just do this in the first page's onLoad() Javascript event using window.open().
There is no newWindow property on a window object (which is what parent references), so this is not unexpected.
Maybe you are looking for the open method instead?
If so, then:
Putting it as the src of an iframe is a very strange thing to do
It will probably be zapped by pop-up blockers
Ok. You try to generate javascript code inside Servlet code. When you do, your code goes to Web browser and it's seen there as a html document with javascript inside. So, your error rather comes from web browser and links to javascript error. Probably it's newWindow method. To open new window you should call window.open() function, I guess.

How can I direct a user to a new page dynamically based on a dropdown list with plain Wicket?

Here is my situation: the user selects a section (for example from a dropdown) such as "Section1," "Section2" or "Section3." Then he clicks the OK button (or some link).
What I need to happen: after he clicks on that button/link, he will be redirected to the selected section, e.g. www.homepage.com/docs#section2.
So far, I have not been able to process the form from Link's onClick method, nor have I been able to call some clickLink on Link from the Button method onSubmit().
I would prefer not to use AJAX or JavaScript. How can I do this?
That's because a Link doesn't submit the form. It just acts as a link to somewhere. To access your formdata you'll need to submit the form first. Try using a SubmitLink instead of a Link and call
getRequestCycle().setRequestTarget
(new RedirectRequestTarget("www.homepage.com/docs#section2"));
from the onSubmit function of the SubmitLink.
Judging from the Javadoc this should work but I can't test it right now.
A RequestTarget that will send a redirect url to the browser. Use this if you
want to direct the browser to some external URL, like Google etc, immediately.
Or if you want to redirect to a Wicket page. If you want to redirect with a
delay the RedirectPage will do a meta tag redirect with a delay.
Did you try Link.setAnchor(Component)?

Categories