How do you access Content of an Iframe using GWT? - gwt

I have a HiddenIframe that i created in GWT which gets response from a post.
OnBrowsewerevent() i try to introspect the contents of Iframe for a error code or success.
When i access the Iframe via GWT, I can access it but innerHTML method always null
I write a JSNI method
private native String getMessage()/*-{
var e = document.getElementById('my_iframe');
var html = e.contentWindow.document.body.innerHTML;
return html
}-*/;
I always get e as null as GetelementbyID is returning null, When i introspect HTMLusing firebug i can see the Iframe with the ID. What is the best way to solve the problem?

You need to use the GWT specific $doc variable, instead of document. From the GWT JSNI documentation:
When accessing the browser's window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page's window and document.

Related

Getting html data from CodeMirror, I get Uncaught ReferenceError: $ is not defined

I get my html data from CodeMirror editor using getValue() that includes my jquery library and then the html data is loaded using the following code:
$("newIframe").contents().find("head").html(headDataTag[0]);
$("newIframe").contents().find("body").html(bodyDataTag[0]);
I checked the head and body of the document in the newIframe and the html data seems correct and render the html, javascript code correctly but not the jquery $(document).ready() code section.
Using Opera debugger and clicking on the network tab, it shows jquery downloaded (locally from a node server) correctly with a http status of 200 ok, however, it does not show under the source tab.
This same html data loads without problems using it in a main code without getting the data from CodeMirror editor.
What I'm doing wrong?
I changed the jQuery code just below:
$("newIframe").contents().find("head").html(headDataTag[0]);
$("newIframe").contents().find("body").html(bodyDataTag[0]);
To pure javascript code just below and everything started working:
var iframeId = document.getEelementById("newIframe");
var iframeDoc = iframeId.contentDocument || iframeId.contentWindow.document;
iframeDoc.open();
iframeDoc.write(editor.getValue) // CodeMirror data from it's editor window
iframeDoc.close();

How to give a url to the page?

I am trying to open a new window with the below code. It opens a new window but it has the url as "about:blank". How to change the this url and give a custom url.
private native void openPrintWindow(String contents) /*-{
var printWindow = window.open("", "PrintWin", false);
printWindow.document.open("text/html","replace");
if (printWindow && printWindow.top) {
printWindow.document.write(contents);
} else {
alert("The print feature works by opening a popup window, but our popup window was blocked by your browser. If you can disable the blocker temporarily, you'll be able to print here. Sorry!");
}
}-*/;
It is empty, because first parameter of window.open method is an empty string. Check some examples here. So it should be something like this:
window.open("https://stackoverflow.com", "PrintWin", false);
From Your code I see You want to open a new window by custom URL with some HTML content inside. You cannot do it this way. If You put some URL, the browser will try to open this URL by making a GET request.
Solution to what You want to achieve is to do it more-or-less the MVC way (please note it is NOT a fully correct MVC solution, just a guidance):
Before You open the window, You need to store a content somewhere (best option is on a server side, but there is also a way to store it on a client side)
Create a new page, accessible via Your custom URL (either a simple HTML or a service, up to Your needs).
You need to write some code in this new page which will retrieve Your content (stored previously somewhere) and present it in this newly opened window.

GWT dynamic data in new browser window at client side

I've got GWT module where I do some stuff and I have search results - doesn't matter in which form. Now after searching, and clicking on for example "Export to HTML" button,I would like to create new html page (for example from client side code by creating simple string which contains only listed results of searching list of results ) and open it in new browser window. I know that there is Window.open(...) method, but there I must specify url which i don't have. I want to create this new html page by client side - without server inference (I don't want to create some resource on server side and then paste url to this resource to client side). Is there any possibility to achieve this? If there is no option, other method which would satisfy me, is to open standard dialog box for saving, which will allow to save results in a html file.
Thanks for helps.
Kind regards.
Here's the code I use to print:
native void openPrintWindow(String contents) /*-{
var printWindow = window.open("", "PrintWin");
if (printWindow && printWindow.top) {
printWindow.document.write(contents);
printWindow.print();
printWindow.close();
} else {
alert("The print feature works by opening a popup window, but our popup window was blocked by your browser. If you can disable the blocker temporarily, you'll be able to print here. Sorry!");
}
}-*/;
Seems like you could adapt it for your purposes with some simple rewording and by removing the call to print()! The contents variable just holds flat HTML. There are no trips to the server.
openPrintWindow("<h1>Search Results</h1><ol><li>etc...");
The method of opening new window from client js which allows user to save that generated content from browser's save as menu is data:url scheme, content written to opened page via println usualy not saved. But data:url works only in morden browsers. And the content written should be quite small to fit browser's url length resteiction.
See example from this article http://en.wikipedia.org/wiki/Data_URI_scheme#JavaScript

Can I access the function body of an event listener using nodeJS jsdom

I am using jsdom with nodeJS. I load in a large HTML document, and am using jQuery to navigate the DOM. I have a case where I have an element, and I need to access the function body of an event listener (onclick). The event listener was added in the source HTML:
The onclick attribute of the DOM element is undefined.
btw: what I really want to do is get the URL (please note that <rest-of-url> is not what is in the source, a real URL spec is there) that is specified in the source.
e.getAttribute('onclick') doesn't work?

can i send data to a javascript function on the server iphone

I i'm wondering if it is possible to call a javascript function from my application. The js function is on the server. Let's say i have a some inputs in the app. Then i have this submit button which calls the IBAction, from the IBAction i want to call:
function mySubmit(){
document.getElementById("myForm").submit();
}
and pass it the data the user entered and then get back a response (in my app) not in the server
Is this possible to do? if so, can you provide some useful links?
Thanks in advance and have a nice saturday ;)
The js function is on the server
Are you sure this is your case? Don't you mean that your js submits a form that is itself treated on the server side?
Javascript is normally a client-side scripting language, which is executed on client-side, contrary to e.g. PHP which is only executed server-side.
[EDIT] (As stated by #Dr.Dredel in the comments, as there exists server-side javascript, but these usages are not yet very common and I don't think it corresponds to your context)
If you need to call a javascript function from an already loaded HTML page in a WebView, you can simply use the UIWebView's stringByEvaluatingJavascriptFromString:. You can pass a string representing javascript code to this function, so you can build a string that represent a function call with string or int arguments for example without any problem.
But if you intend to load your HTML page just to call a javascript fonction which in turn submit a form that sends the form's data to your server… This is clearly the wrong way!
You should instead considering performing the NSURLRequest to your server directly in Objective-C (maybe using ASIHTTPRequest to perform a POST request and easily set the values for each keys of the form you intend to send). In addition, doing this directly in ObjC will avoid loading the HTML page for nothing and rely on the js script, and will allow you to directly get the response in the delegate method.