javascript - 从android webview中的网页重定向

标签 javascript android html webview redirect

所以我随身携带了这张表格。我想将其包含在我的 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>

这在网络上工作得很好,但是当我尝试将其加载到我的 WebView 中时,重定向不会发生。我有什么办法可以实现这个目标吗?我还在下面发布了我的 Android 代码。 webview 加载良好..唯一的问题是重定向

Android 代码

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);
}

}

请帮助我解决我做错的事情或我遗漏的内容

最佳答案

尝试使用

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

此外,如果您想强制重定向到特定页面,请重写 shouldOverrideUrlLoading() 方法。

@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
       }
});

这将在加载网页后立即加载网址。您还可以使用计时器来显示静态网页,然后将其重定向到新页面。

关于javascript - 从android webview中的网页重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097222/

相关文章:

javascript - 为什么我的 Javascript 表单验证不能正常工作?

android - 在 Android 中使用 Apache Commons lib 时的奇怪行为

android - 如何从不同的类调用 onDraw?

javascript - 从 Angularjs 列表中删除图像

javascript - 创建 div 元素并在其上动态添加子元素

javascript - offset().top 在 Safari 中不起作用

android - 滑动并展开 View Android

javascript - 检查文本的表单输入并更改其颜色

javascript - onhashchange 事件导致无效的标记验证

javascript - 使用 Strophe.js 自定义 XMPP 消息