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)
Related
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!
Hi I have a problem the codes does not work on IE8 and IE9 but working with higher version and other browsers like chrome. The page does not reload on IE8 and IE9 only.
Scenario I'm doing when I choose 1 option on the radio button. the panel should reload.
This is my java code.
ProfileBasePage.java
// Javascript used to resize the Modal page after hiding/showing components.
Public static String POPUP_RELOAD_JS =
"var iFrame = window.parent.document.getElementsByTagName('iframe')[0];"
+ "(iframe.onload)();";
/** Calls the Javascript code to reload the modal panel only when the popup parameter is set to true.*/
protected void reloadPopupiFrame(AjaxRequestTarget target)
{
//POPUP_PARAMETER="popup"
String popup = getRequest().getRequestParameters().getParameterValue(ProfileBasePage.POPUP_PARAM_KEY).toString();
//TRUESTRING = "true"
//append the javascript used to reload the modal iFrame.
if(ProfileBasePage.TRUESTRING.equalsIgnoreCase(popup))
{
target.appendJavaScript(POPUP_RELOAD_JS);
}
}
If you're using Wicket ModalWindow class, you can resize it with Wicket very own JavaScript command:
target.appendJavaScript("window.parent.Wicket.Window.current.autoSizeWindow();");
(iframe.onload)() looks fishy. Maybe older IE versions do not support it.
Better use a proper function call instead. I.e. your iframe should look like: <iframe onload="myOnLoadOrResizeHandler()"...> and then the JS code executed by Wicket should be: target.appendJavaScript("myOnLoadOrResizeHandler();").
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.
I am trying to create some sort of workflow in GreaseMonkey.
I start with GreaseMonkey defining jQuery if it isn't already defined:
/*! jQuery v1.7.1 jquery.com | jquery.org/license */
if (typeof jQuery == 'undefined')
(function(a,b).......function(){return f})})(window); // packed version
jQuery.noConflict();
/*! end of jQuery */
Then, it periodically checks a web page (jQuery.ajax/type=get/url:window.location.href).
When some condition is met, a window is created using
var url = <some page on the same domain>
var opened = window.open(url, "XYZ");
The question is how to get a button on the opened window to click. Let's say on the page there is a button
<input type="button" id="clickme"
I've tried the obvious such as
opened.document.getElementById('clickme').click()
jQuery(opened.document).find('#clickme').click()
But neither work. This is probably a GreaseMonkey issue so would like to see if anyone has something similar working. My current workaround is to set up another GreaseMonkey script against the opened url which clicks the button if window.name = 'XYZ'.
The question is pretty vague; I'm assuming you're creating a window using window.open or something to that effect, and want to interact with the DOM inside.
You should be able to use the reference to the newly-opened window as you would use the window variable in regular JavaScript:
window.document.getElementById(...).doStuff(...)
becomes
var thatWindowReference = window.open(...);
...
thatWindowReference.document.getElementById(...).doStuff(...)
If you've got jQuery loaded inside the window, then you can do
thatWindowReference.$('selector').doStuff(...)
As for how to simulate a click on a button inside that window:
thatWindowReference.document.getElementById('thatButton').click();
or using jQuery:
thatWindowReference.$('#thatButton').click();
Edit: Without using jQuery from inside the new window (test it out on jsFiddle):
var w = window.open('about:blank');
w.document.write('<button id="test" onclick="alert(\'I was clicked\');">Hello</button>');
$(w.document.body).find('#test').click();
Is there anyway to set the title of a website via JS onload? I wrote this but I'm not sure where it's incorrect:
function my_code(){
document.title = "The new title goes here.";
}
window.onload=my_code();
The page title is static by the way.
Edit: The reason why I want to do it this way is because I'm writing a Safari Extension for a website that does not include tags so I wanted to insert one via JS.
You don't need to wait for the load event.
Just write document.title = ... anywhere.
If you want to change the title after the page loaded, you have to assign a function reference to onload (your function assigns the return value of my_code):
window.onload = my_code;
However, you most certainly can set the title without waiting for the load event.
I'm not familiar with Safari extensions, but you should also make sure that window actually refers to the page's window.