logo
down
shadow

Why JavaScript dialogs (alert/prompt/confirm) are not widely used and not under active development?


Why JavaScript dialogs (alert/prompt/confirm) are not widely used and not under active development?

By : djtuxy
Date : November 22 2020, 07:01 PM
hope this fix your issue alert displays a modal dialog box which effectively disables the browser UI until it is dismissed. Most developers consider that to be bad design and most users consider that irritating.
One of the main issues that I find with modal dialogs is that they don't let me open a new tab and do a quick Google search before responding to them.
code :


Share : facebook icon twitter icon
How to make Alert, Prompt and confirm dialog of javascript in java Android

How to make Alert, Prompt and confirm dialog of javascript in java Android


By : manikandan basav
Date : March 29 2020, 07:55 AM
will be helpful for those in need I want to make a confirm function in java just like a alert in javascript. ,
Example javascript code: var a = prompt("");
Make javascript Alert/Confirm dialogs more visible (especially in the Google Chrome browser)

Make javascript Alert/Confirm dialogs more visible (especially in the Google Chrome browser)


By : Yiannis Tsesmelis
Date : March 29 2020, 07:55 AM
Hope that helps One option is to create your own Alert, using div overlays. However, doing that for Confirm dialogs would be more difficult, since you often want all execution to stop until the user chooses OK or Cancel on the Confirm dialog.
A great option is the following, which allows you to continue using the browser's Alert and Confirm dialogs:
code :
function alrt(msg) {
    var tint = $('<div class="PopupBgTint"></div>');
    tint.appendTo('body');
    alert(msg);
    tint.remove();
}
function cnfrm(msg) {
    var tint = $('<div class="PopupBgTint"></div>');
    tint.appendTo('body');
    var rtrn = confirm(msg);
    tint.remove();
    return rtrn;
}
.PopupBgTint
{
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    background-color: black;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
    filter: alpha(opacity=50);
    -moz-opacity: 0.5;
    -khtml-opacity: 0.5;
    opacity: 0.5;
    z-index:99999;
}
how to know whether modal boxes (alert, prompt, confirm...) have been disabled in javascript?

how to know whether modal boxes (alert, prompt, confirm...) have been disabled in javascript?


By : Gabriel Barros
Date : March 29 2020, 07:55 AM
this one helps. When that box is checked, the dialog "closes" immediately. You could check to see if the box closes unusually fast:
code :
function dialog(message, success, failure) {
    var open_time = new Date();
    var result = alert(message);
    var close_time = new Date();

    if (close_time - open_time < 10) {
        failure();
    } else {
        success(result);
    }
}

dialog('Hello', function(result) {
    // The dialog probably was closed by the user
}, function() {
    // The dialog was closed really fast.
    // Either the user was typing while it popped up or the browser didn't
    //  display it in the first place
});
WKWebView javascript alert, prompt, confirm won't work

WKWebView javascript alert, prompt, confirm won't work


By : Tc1977
Date : March 29 2020, 07:55 AM
Any of those help okay, so I found answers and solution,
1) this will add support for native JS methods. alert(), prompt() and confirm() could be called from JS or via
code :
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("confirm('Hello from evaluateJavascript()')", completionHandler: nil)
    }
func webView(_ webView: WKWebView,
                 runJavaScriptAlertPanelWithMessage message: String,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping () -> Void) {

        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        let title = NSLocalizedString("OK", comment: "OK Button")
        let ok = UIAlertAction(title: title, style: .default) { (action: UIAlertAction) -> Void in
            alert.dismiss(animated: true, completion: nil)
        }
        alert.addAction(ok)
        present(alert, animated: true)
        completionHandler()
    }

func webView(_ webView: WKWebView,
                 runJavaScriptTextInputPanelWithPrompt prompt: String,
                 defaultText: String?,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (String?) -> Void) {

        let alert = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)

        alert.addTextField { (textField) in
            textField.text = defaultText
        }

        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
            if let text = alert.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler(defaultText)
            }

        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in

            completionHandler(nil)

        }))

        self.present(alert, animated: true, completion: nil)

//        if ipad will crash on this do this (https://stackoverflow.com/questions/42772973/ios-wkwebview-javascript-alert-crashing-on-ipad?noredirect=1&lq=1):
//        if let presenter = alertController.popoverPresentationController {
//            presenter.sourceView = self.view
//        }
//        
//        self.present(alertController, animated: true, completion: nil)
    }

func webView(_ webView: WKWebView,
                 runJavaScriptConfirmPanelWithMessage message: String,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (Bool) -> Void) {

        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
            completionHandler(true)
        }))

        alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
            completionHandler(false)
        }))

        self.present(alertController, animated: true, completion: nil)
    }
webView.uiDelegate = self
webView.navigationDelegate = self
view.addSubview(webView!)
import UIKit
import WebKit

class ViewController:
    UIViewController
    , WKNavigationDelegate
    , UIScrollViewDelegate
    , WKUIDelegate
{

    //wk webvew set

    @IBOutlet var webView: WKWebView!
    let getUrlAtDocumentStartScript = "GetUrlAtDocumentStart"
    let getUrlAtDocumentEndScript = "GetUrlAtDocumentEnd"
//webkit.messageHandlers.GetUrlAtDocumentEnd.postMessage('1')
    
    override func loadView() {
        self.webView = WKWebView()
        self.webView.navigationDelegate = self
        
        //for prompt ??
        self.webView?.uiDelegate = self

        view = webView
    }
    
    override func viewWillAppear(_ animated: Bool) {//white status bar
        super.viewWillAppear(animated)
        webView.isOpaque = false //removes white flash on WKWebView load
        webView.backgroundColor = UIColor(red: 41/255, green: 45/255, blue: 91/255, alpha: 1)
        UIApplication.shared.statusBarStyle = .lightContent
        
        do {

            let paid = Bundle.main.infoDictionary?["paid"]  as? Bool;
            var fileName = "none"

            if(paid!){
                fileName = "index-ios-wvd-inlined--paid"
            } else {
                fileName = "index-ios-wvd-inlined"
            }
            guard let filePath = Bundle.main.path(forResource: fileName, ofType: "html")
                else {
                    // File Error
                    print ("File reading error")
                    return
            }


            let contents =  try String(contentsOfFile: filePath, encoding: .utf8)
            let baseUrl = URL(fileURLWithPath: filePath)
            webView.loadHTMLString(contents as String, baseURL: baseUrl)
            
        }
        catch {
            print ("File HTML error")
        }
        
    }
    
    override var preferredStatusBarStyle : UIStatusBarStyle {//white status bar
        return .lightContent
    }
    
    override func viewDidLoad() {
        webView.scrollView.bounces = false;
        
        super.viewDidLoad()
        
        //disable zoom
        webView.scrollView.delegate = self
        
        
        
        let config = WKWebViewConfiguration()
        config.addScript(script: WKUserScript.getUrlScript(scriptName: getUrlAtDocumentStartScript), scriptHandlerName:getUrlAtDocumentStartScript, scriptMessageHandler: self, injectionTime: .atDocumentStart)
        config.addScript(script: WKUserScript.getUrlScript(scriptName: getUrlAtDocumentEndScript), scriptHandlerName:getUrlAtDocumentEndScript, scriptMessageHandler: self, injectionTime: .atDocumentEnd)
        webView = WKWebView(frame:  UIScreen.main.bounds, configuration: config)
        webView.navigationDelegate = self
        view.addSubview(webView)

        
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view.addSubview(webView!)
         
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    func viewForZooming(in: UIScrollView) -> UIView? {
        return nil;
    }

    //disable zoom
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tapped(i:Int) {
        print("Running \(i)")
        
        switch i {
        case 1:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.error)
            
        case 2:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.success)
            
        case 3:
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.warning)
            
        case 4:
            let generator = UIImpactFeedbackGenerator(style: .light)
            generator.impactOccurred()
            
        case 5:
            let generator = UIImpactFeedbackGenerator(style: .medium)
            generator.impactOccurred()
            
        case 6:
            let generator = UIImpactFeedbackGenerator(style: .heavy)
            generator.impactOccurred()
            
        default:
            let generator = UISelectionFeedbackGenerator()
            generator.selectionChanged()
        }
    }
    
    
    //default alert/confirm/prompt dialogs
    func webView(_ webView: WKWebView,
                 runJavaScriptAlertPanelWithMessage message: String,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping () -> Void) {
        
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        let title = NSLocalizedString("OK", comment: "OK Button")
        let ok = UIAlertAction(title: title, style: .default) { (action: UIAlertAction) -> Void in
            alert.dismiss(animated: true, completion: nil)
        }
        alert.addAction(ok)
        present(alert, animated: true)
        completionHandler()
    }
    
    func webView(_ webView: WKWebView,
                 runJavaScriptTextInputPanelWithPrompt prompt: String,
                 defaultText: String?,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (String?) -> Void) {
        
        let alert = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)
        
        alert.addTextField { (textField) in
            textField.text = defaultText
        }
        
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
            if let text = alert.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler(defaultText)
            }
            
        }))
        
        alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
            
            completionHandler(nil)
            
        }))
        
        self.present(alert, animated: true, completion: nil)
        
//        if ipad will crash on this try to uncomment (based on https://stackoverflow.com/questions/42772973/ios-wkwebview-javascript-alert-crashing-on-ipad?noredirect=1&lq=1):
//        if let presenter = alertController.popoverPresentationController {
//            presenter.sourceView = self.view
//        }
//        
//        self.present(alertController, animated: true, completion: nil)
    }
    
    func webView(_ webView: WKWebView,
                 runJavaScriptConfirmPanelWithMessage message: String,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (Bool) -> Void) {
        
        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
        
        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
            completionHandler(true)
        }))
        
        alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
            completionHandler(false)
        }))
        
        self.present(alertController, animated: true, completion: nil)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("confirm('Hello from evaluateJavascript()')", completionHandler: nil)
    }
    
}

extension ViewController: WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        switch message.name {
            
        case getUrlAtDocumentStartScript:
            tapped(i: message.body as! Int)
            //print("start: \(message.body)")
            
        case getUrlAtDocumentEndScript:
            tapped(i: message.body as! Int)
            //print("tapped: \(message.body)")
            
        default:
            break;
        }
    }
}

extension WKUserScript {
    class func getUrlScript(scriptName: String) -> String {
        return "webkit.messageHandlers.\(scriptName).postMessage(1)"
    }
}

extension WKWebView {
    func loadUrl(string: String) {
        if let url = URL(string: string) {
            load(URLRequest(url: url))
        }
    }
}

extension WKWebViewConfiguration {
    func addScript(script: String, scriptHandlerName:String, scriptMessageHandler: WKScriptMessageHandler, injectionTime:WKUserScriptInjectionTime) {
        let userScript = WKUserScript(source: script, injectionTime: injectionTime, forMainFrameOnly: false)
        userContentController.addUserScript(userScript)
        userContentController.add(scriptMessageHandler, name: scriptHandlerName)
    }
}
An alternative to alert/confirm/error dialogs?

An alternative to alert/confirm/error dialogs?


By : user3741671
Date : March 29 2020, 07:55 AM
I hope this helps you . Here’s the hierarchy for dealing with user errors or warnings, applied to validating user input to a field.
Related Posts Related Posts :
  • setting a value through jquery
  • When to use Javascript object literals
  • Is it possible to resize text to fit a fixed size div?
  • Javascript unset array
  • Should a "script" tag be allowed to remove itself?
  • How to use outerHTML in JavaScript?
  • How do you know if an object is JSON in javascript?
  • Javascript and backslashes replace
  • Click ID and open URL?
  • Center an Image vertically and horizontally using CSS
  • Sorting objects by property values
  • javascript number split
  • How can I upload a file?
  • how can put a javascript variable in img tag
  • Passing Variable in load
  • How do I move focus to next input with jQuery?
  • Reordering arrays
  • How can I read the value of a radio button in JavaScript?
  • what is name parameter in window.open()
  • How to add and run a javascript for only on firefox not on others?
  • reading the current url of a tab
  • How can I get selector from jQuery object
  • Javascript Browser Issue
  • Javascript compiled or not? Check inside
  • Create a Count Up Timer in Javascript/Jquery
  • In Javascript, what's better than try/catch for exiting an outer scope?
  • Using JavaScript regex to strip substring that contains parentheses
  • jquery split() and indexOf results in "Object doesn't support this property or method"
  • ActionScript to Javascript cross-compiler
  • TinyMCE and pluploader not working together
  • Writing to a new window with javascript... get access denied
  • Differences between testing JavaScript and testing Ajax?
  • What JavaScript functions are supported by different browsers?
  • passing input text value to ajax call
  • Can JavaScript load raw bytes to use in HTML5 Canvas?
  • html form.input.value is not getting printed why?
  • A good way to redirect with a POST request?
  • Detect blocked pop-up in Chrome
  • javascript location.hash refreshing in IE
  • Sproutcore: adding a custom stylesheet or script
  • Display tabs like FireFox's on my website
  • JavaScript - get detailed information about the browser
  • Javascript mouse event not captured properly when mouse moved very fast
  • Large number array compression
  • Is it possible to use a string variable to reference an element in Javascript?
  • remove params from form request
  • How to prevent direct access to my JSON service?
  • Trigger Div Click From Link
  • JavaScript multithreading in IE6?
  • Variable scoping and the jQuery.getJSON() method
  • How can I Mimic Gmail's Basic HTML option
  • jQuery.ui.draggable.js and jQuery.ui.widget.js conflict
  • Row rendering in Qooxdoo
  • Google Maps: How does Trulia create their custom InfoWIndows?
  • How can I create an automatically generated alteranate table row color?
  • this fadeIn / fadeOut jQuery code crashes my browsers- Why?
  • call a server side python script from javascript
  • Code Colorer Being Used
  • dynamically create element using javascript
  • jQuery to check if returned data object is empty
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk