I am making a hybrid app. I am using xamarin for android, pretty much the same as android. I have already figured out how to hook the phone's back button press. When pressed I want my app code to either mimic or call a javascript function that is part of the webpage that the webview is displaying. It is my web page, so I know the code that the webview is rendering. To be specific, I want to call a jquery slideToggle function on a page element when the phone's back button is pressed. Can that be done, and if so what would be the best approach? I'm hoping that someone here has had to do something just like this in the past. Thanks.
I don't think webview supports JQuery, unless maybe you reference it.
The Xamarin android way of doing it is:
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
Source : Android.Webkit.WebView.AddJavascriptInterface Method
Also make sure you have enabled Javascript,
web_view.Settings.JavaScriptEnabled = true;
You can show or hide the element by finiding it by id (document.FindElementById). Then you can set its display to block or none. Thus you can do it purely in Javascript.
Add on :
There are couple of problems with your method. To name a few -
on pressing of back button user expects a particular behavior and it should not be altered. This would not give a rich user experience.
if you are override ing the back button press then what about the navigation bar menu click.
JavaScript way of doing can cause security issues.
Related
My iOS app uses a WKWebView with contenteditable = true on a specific div. I'd like to have code to make the keyboard show up for the web view, so the user can just start typing. Things I've tried that have had no effect:
Telling the web view to becomeFirstResponder (a long shot, because the web view wouldn't know what div to use).
Injecting JS to tell the div to focus(). (This works in other browsers, but sadly not in WKWebView)
Simulating touch events in JS via TouchEvent and dispatchEvent() in the hope of making it seem that the user had tapped on the div.
In the third case I also used addEventListener() to observe the simulated touches and compare them to real touch events from tapping the screen. It looks like the key difference is that the event's isTrusted value is false for the simulated touches.
I get that it's a potential security issue to let apps simulate touch events, but I didn't have any other ideas. I'm trying to get the keyboard to appear, what the user types is up to them and not something I want to mess with. Basically I want the same thing as calling becomeFirstResponder() on a UITextView.
This is very similar to a WebKit issue 142757 but I haven't figured out how to use the suggested workaround linked from there.
Clarification: I can set up and use an editable web view, but the keyboard doesn't appear until I tap on the web view. I'm trying to make the keyboard appear automatically, without requiring a tap to initiate editing.
I tried this in an iPad playground, and it works without any action on my part. It’s possible there is another view that is capturing touches, or “contenteditable” is misspelled, or something else?
So I have this website I made for fun a while back: fakebsod.com
I made it so clicking on the first block in the instructions makes the browser go full screen using a function toggleFullScreen(elem). This is called on document.body. My issue is that I want a person to be able to click on the first block and then on the second and still be in fullscreen mode.
I don't want really want to use AJAX or anything like that to load the http://fakebsod.com/generic page so what is a nice way of keeping the page fullscreen?
I can't use parameters or anything as the browsers requires a user to trigger toggleFullScreen via a click or action (I can't just call it).
So any idea on how I can do this?
(Yes I know going fullscreen manually is easy but I want to do this anyway).
I have made a game in Javascript that I want to make into an Android app.. So obviously using a webView..
I have a function in Javascript that says if you click on the left hand side of the page, the box will move left - if right it will move right etc.
My question is if on my Android phone I click on a specific area of the webView, will it be the same as if I click from a computer?
The reason I am asking rather than showing what I have done, is that I don't want to implement it into Eclipse to find the idea was redundant.
A WebView is like a browser window.
So open your page into the android browser and test, if it works as you wish there, it will most likely work about as good in the WebView.
Don't forget to enable JavaScript for the WebView:
webview.getSettings().setJavaScriptEnabled(true);
Salutations,
I am working on a project that requires that I control a JavaScript-heavy website from an application created in Visual Basic.
I have been fairly successful with using webBrowser.navigate("javascript:blahblahblah") to login and change values on the website's controls. This would be fine, however the site uses an alert window after I do the injection to log in. Users will not want to see that. I can change the opacity and visibility for the vb frame, but the javascript alert window cannot be hidden using the properties of the frame and the webBrowser control.
So, I'm looking for a way to override the alert function via JavaScript injection (or some other way if possible). I've tried:
webBrowser.Navigate("javascript:function alert() {};")
webBrowser.Navigate("javascript:window.alert = function() { };")
webBrowser.Navigate("javascript:alert = function{};")
And the alert window still appears.
Any ideas on how to stop the alert windows from appearing?
Thanks.
Instead of webBrowswer.Navigatetry webBrowser.Document.InvokeScript. Reference
i have developped a mobile app with dojo and phonegap for android. The problem is, that I want to acces the bottom right corner button. I want to change its name to "send" and I want to connect the button with a function. Does anyone have an idea how to do that? Using javascript, or how to do that natively?
Natively it is possible to change the text to certain other limited options however I doubt whether it is possible to do from the context of a PhoneGap app.
Check this post which details the process of doing relatively same but in native.
Android - Handle "Enter" in an EditText
For more detail you need to check the doc