简体   繁体   中英

Redirection from a webpage in android webview

So I have this form with me. which I would like to include in my webview.

    <html>
    <head>
    <meta name="viewport" content="user-scalable = yes">

    <link rel="stylesheet" href="assets/css/bootstrap.css">
    </head>
    <body>
           <!--LOGIN FORM -->
    <div class="border" Style="width:100%;max-width:100%;height:100%;">
    <div class="bs-example form-horizontal">
    <div id="msg-info" style="display:none;" ></div>
      <form class="form-horizontal" id="login-user" name="login-user" action='' method="POST">

        <fieldset>

        <div id="legend">

            <legend class="">Reigstered Members Login<span class="text-center">&nbsp;&nbsp;&nbsp;<img id="spinner" src="images/loading.gif" style="display:none;"/></span></legend>

        </div>

        <div class="control-group">

            <!-- Username -->

            <label class="control-label" for="username" style="text-align:left;"><strong>User ID</strong>&nbsp;<small>(It can be Account No, Mobile No or Email)</small></label>

            <div class="controls">

            <input type="text" id="username" name="username" placeholder="" class="form-control">

            </div>

            <!-- Password-->

            <label class="control-label" for="password" style="text-align:left;"><strong>Password</strong></label>

            <div class="controls">

            <input type="password" id="password" name="password" placeholder="" class="form-control">

            </div>

            <label class="control-label" for="password">&nbsp;</label>
            <!-- Button -->

            <div class="controls"> 
                <input type="button" id="login-btn" name="login" class="btn btn-success" value="Login &raquo;" />&nbsp;&nbsp;&nbsp;&nbsp;
            </div>
          </div>  
         </fieldset>
        </form>
      </div>

            <p><font color="red">Note:</font> Those of you who have not yet registerd can register from here, Note you have to update your mobile no with the society so that registration can be completed</p> 


      </div>
    <br><br><br><br>
<script>
$("#login-btn").click(function(){
    validateForm();
});
$("#login-user").keypress(function(event){
    if(event.keyCode == 13  || event.which == 13){
        validateForm();
    }
});
function validateForm(){
    var flag=true;
    if($("#username").val()==''){
        $("#msg-info").addClass("alert alert-danger");
        $("#msg-info").html('Enter valid User ID.');
        $("#msg-info").show("slow");    
        flag=false;
        return false;
    }
    if($("#password").val()==''){   
        $("#msg-info").addClass("alert alert-danger");
        $("#msg-info").html('Enter valid Password.');
        $("#msg-info").show("slow");
        flag=false;
    }
    if(flag){
        $("#msg-info").hide("slow");
        //
        $('#spinner').show();
        jQuery.post('validations/loginStore.php', $('form[name=login-user]').serialize(), function(data) {
            data=JSON.parse(data);
            $("#msg-info").removeClass("alert-danger");
            if(data.update){
                //redirect to secure page
                if(data.role=='admin'){
                    window.open('app/dashboard.php?page=societylist&profile=dash', '_parent');
                } else {
                    window.open('app/dashboard.php?page=home', '_parent');
                }
            } else {
                $("#msg-info").addClass("alert alert-danger");
            }
            if(typeof(data.msg)!='undefined' && data.msg !='' && !data.update){

                    $("#msg-info").html(data.msg);
                }
           $("#msg-info").show();
           $('#spinner').hide();
        });
    }
}
</script>
        </body>
         </head>

This works perfectly fine on the web but when I try to load it in my webview, the redirection does not happen. Any way I can achieve this? I am also posting my android code below. The webview loads fine.. The only issue is with redirection

Android Code

public class LoginActivity extends ActionBarActivity {

WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setLoadsImagesAutomatically(true);
    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    myWebView.loadUrl("http://www.chsonline.in/api/login-frame.php");



    myWebView.setWebViewClient(new WebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

Kindly assist me on what I am doing wrong or if I am missing something

You are seems to be having a related problem as following links. 1. Opening webview not in new browser 2. parent.opener doesn't work in webview Android This may help you.

Try using

myWebView.getSettings().setPluginState(PluginState.ON); // this is for newer API's

Also if you want to force redirect to a specific page, override the shouldOverrideUrlLoading() method.

@Override
    myWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            // do your handling codes here, which url is the requested url
            // probably you need to open that url rather than redirect:
            view.loadUrl(url);
            return false; // then it is not handled by default action
       }
});

This will load the url right after loading the webpage. You can also use a timer to show your static web page and then redirect it to a new page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM