I am working on a web application which will be accessed from iOS UIWebView. When user touches an input text field, the UIWebView automatically opens the keyboard. Everything is fine up to this point.
Now when user taps anywhere on the page I want to dismiss the keyboard. What would be the method to do this.
I tried following code but that did not work for me -
$(document).on("tap", function (e) {
document.activeElement.blur();
});
Try something like
document.foo.bar.myinput.blur();
I think it will help you out.
[myWebView stringByEvaluatingJavaScriptFromString:#"document.activeElement.blur()"];
If you want a non-Javascript option:
Start by adding a UITapGestureRecognizer to your UIWebView in your ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissWebViewKeyboard)];
[self.webView addGestureRecognizer:tapGesture];
//the rest of your initialization code goes here
}
And then, add your dismissWebViewKeyboard method, which just ends editing on your UIWebView:
- (void)dismissWebViewKeyboard
{
[self.webView endEditing:YES];
}
And that should do it!
Related
I just started to develop with xCode because i need a WebView container for a website.
It works so far but i can't logout on the website because there is (in a web browser) a popup window asking if i was sure to logout. I guess it is creates with javascript.
In the Web View settings there is a checkbox labeled "allow popups" but in my app no popup appears after the click.
I've searched for two hours and didn't find something similar related to my problem.
It was a confirm() function of javascript.
I got it to work with:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[webView setUIDelegate:self]; // <------- This is an important part!!!
[[webView preferences] setJavaScriptEnabled:YES];
[[webView preferences] setJavaScriptCanOpenWindowsAutomatically:YES];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:homeURLString]]];
}
and
- (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
NSInteger result = NSRunInformationalAlertPanel(NSLocalizedString(#"JavaScript", #""), // title
message, // message
NSLocalizedString(#"OK", #""), // default button
NSLocalizedString(#"Cancel", #""), // alt button
nil);
return NSAlertDefaultReturn == result;
}
More Info here and here.
Actually I am using the following code for closing my current window:
window.close()
The thing is, it is working fine in IE, but it is not working in FF and Chrome.
Is their any great solution for doing this?
function windowclose(w) {
try {
if (dojo.isIE>=7) {
w.open('', '_self', '');
w.close();
} else if (dojo.isIE==6) {
w.opener = null;
w.close();
} else {
if(!w.opener)
w.opener = 'x';
w.close();
}
} catch(e) {
alert("To avoid data corruption/loss, please close this window immediately.");
}
}
To be used as:
windowclose(window)
I found a partial solution, Java is your friend. It works in a button should work in a link too. The only problem is when called from an event like onClose the current xpage looses focus and the current pages stays open. I tried to emulate send keys and it presses the ESC key. It works fine from a button in.
Button on CLick event
<xp:button value="Label" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:var robot:java.awt.Robot= new java.awt.Robot;var event:java.awt.event.KeyEvent=java.awt.event.KeyEvent;
robot.keyPress(event.VK_ESCAPE);
robot.keyRelease(event.VK_ESCAPE);
enter code here}]]>
</xp:eventHandler>
</xp:button>
window.close() works in 8.5.3 but only if parent contains an object and it will not do this if you have i.e an xpage that is opened inside an ordinary Notes application or a ordinary view. you need a window.open to get this.
I have investigated alot about this a while ago but no luck finding an answer. The only way I found is that you need the Mindoo XPage2Esclipse plugin to get this to work.
You may have to call window.focus() before calling window.close() in Firefox
I use window.close with no problems in 8.5.3 apps with Chrome / FF / IE.
In the main page of an app, the "Case Document" I have some CSJS at the top that names the page ie.
window.name="mainWindow";
and then I have a button that allows you to ask a question - this pops up a new window/tab and keeps the main case doc open as well. In the new window, there's a submit button that does a full update and in the onComplete event I have the following CSJS to update the main doc so you can see the question on the main doc in the repeat control that shows the threads of Q & A docs:
if (window.opener!=null){
window.opener.location.href = window.opener.location.href;
window.close();}
else {
alert("Can't refresh parent case doc - have you closed the window?");
}
Hope this helps
A customer needs that an alert will show to the user when they try to print (either via Ctrl+P or File/Print) anything from the browser, such as "Remember to not print more than x pages" or something like that. Is it possible, using Javascript, CSS or any other approach?
Unfortunately no. You can call the print dialog from JavaScript, but you can not intercept it.
The only way would be to do something like this would be with ActiveX on internet explorer only, which is just plain ugly.
It's not possible. A sort of workaround could be to open the relevant page in a separate window without menu. So the user is not able to click File->Print directly. If you want to be sure you might also capture right mousebutton and prevent the context menu from opening.
Then you will include a "print page" link with the javascript including your alert.
Not very elegant - I know.
There is a solution with window.onbeforeprint/window.matchMedia
function beforePrint() {
alert('Remember print only X copies');
}
function afterPrint() {
//do something
}
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
http://jsfiddle.net/mowglisanu/FY4q2/
Detecting Print Requests with JavaScript
For some reason beforeprint fires twice in Chrome though.
however you can use css to stop printing
<style type="text/css" media="print">
body{visibility:hidden;} or use display:none;
</style>
I am using UIWebView to load a URL.
Inside the page of that URL, it uses alert("whatever msg") as JavaScript. My UIWebView will pop up a window and show that alert message.
Is there a way to disable this kind of popup window or JavaScript alert window?
Add this after your web view has loaded its content
[MyWebView stringByEvaluatingJavaScriptFromString:#"window.alert=null;"];
You can bind window.alert to another function. So:
window.alert = function() {
//does nothing so effectively "disables" alert
};
Make sure you do this before you call any alerts. The neat thing about this is you can customize the way you display messages to the user. So you could override window.alert to log to the console (for debugging purposes) or you can render it on the page (with a lightbox or something similar).
Since a UIWebView translates all Javascript alerts into native UIAlertViews it is fairly simple to block it on the native end. Looking into UIAlertView.h there is only one public method for showing an alert which is conveniently called: - (void)show;.
#interface UIAlertView (Blocker)
#end
#import "UIAlertView+Blocker.h"
#implementation UIAlertView (Blocker)
- (void)show {
return;
}
#end
You can find the answer here: https://stackoverflow.com/a/21698251/2377378
let script = """
window.alert=window.confirm=window.prompt=function(n){},
[].slice.apply(document.querySelectorAll('iframe')).forEach(function(n){if(n.contentWindow != window){n.contentWindow.alert=n.contentWindow.confirm=n.contentWindow.prompt=function(n){}}})
"""
webView.evaluateJavaScript(script, completionHandler: nil)
I have a UIWebView and when I press a textual link (for at least a second) some kind of UIActionSheet appears with 3 buttons (open, copy and cancel) - Is there a way to prevent it from appearing?
Thanks
You can achieve this using CSS:
* {
-webkit-touch-callout:none;
}
Apparently no. In the documentation, neither UIWebView nor UIWebViewDelegate expose some method or property to avoid this behavior.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:#"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"];
}