ورود

View Full Version : مشکلات WebView با بعضی توابع جاوا اسکریپت



tam2145
یک شنبه 18 مرداد 1394, 16:50 عصر
سلام دوستان
من یک سایت دارم که نسخه موبایل جداگانه دارد . حالا قصد دارم برای آن اپلیکیشن بسازم تا دوستان بدون وارد کردن ادرس مستقیما به نسخه موبایل سایتم وصل شند .
مرورگر را با WebView در اندروید استادیو ساختم . بهش دسترسی به اینترنت و جاوا اسکریپت هم داده ام ولی توی بعضی توابع جاوا اسکریپت مشکل دارد . هر چی هم توی گوگل و ... سرچ کردم هر کدی هم بود تست کردم ولی مشکلم رفع نشد . گفتم بیام از دوستان ایرانی متخصص سوال کنم ، ان شاءا... مشکلم حل میشود . http://p30droid.com/images/smilies/angel.gif

اولین تابع جاوا اسکریپت :


function NewWindow(mypage,myname,w,h,pos,infocus){
if(pos=="random"){
myleft=(screen.width)?Math.floor(Math.random()*(sc reen.width-w)):100;mytop=(screen.height)?Math.floor(Math.rand om()*((screen.height-h)-75)):100;
}
if(pos=="center"){
myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;
}else if((pos!='center' && pos!="random") || pos==null){
myleft=0;mytop=20
}
settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=yes,location=no,directories=no,status= no,menubar=no,toolbar=no,resiz ​able=yes";
win=window.open(mypage,myname,settings);
win.focus();
}



با window.open مشکل دارد و یک پنجره داخلی باز نمیکند و لینکی که بهش ارسال میشود را به صورت یک لینک بارگزاری میکند . یعنی WebView به یک ادرس دیگر می رود .

مشکل بعدی با تابع جاوا اسکریپت زیر می باشد :


function post_delete(postid, confirm_msg){
var req = ajax_init(false);
if( ! req ) { return; }
if( confirm_msg ) {
if( ! confirm(confirm_msg) ) { return; }
}
var thislink = d.getElementById("postlink_delete_"+postid);
req.onreadystatechange = function() {
if( req.readyState != 4 ) { return; }
if( req.responseText != "OK" ) { return; }else{
alert('ارسال موردنظر بدرستی حذف شد.');
window.location = siteurl;
}
}
req.open("POST", siteurl+"ajax/delpost/r:"+Math.round(Math.random()*1000), true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send("postid="+encodeURIComponent(postid));
thislink.style.cursor = "wait";
}



متاسفانه پست را حذف نمی کند و حدس خودم ناسازگاری با توابع alert و window.location می باشد . برای مشکل window.location فکر کنم لینک زیر مشکل را حل کند :




http://www.chrislinford.co.uk/get-going-guides/add-ons/iphoneandandroid.html




کد های main.activity.java هم به شرح زیر است :



package *****;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
//import android.annotation.SuppressLint;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.AlertDialog;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//@SuppressLint("SetJavaScriptEnabled")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);

WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true);
//webSettings.setJavaScriptCanOpenWindowsAutomatical ly(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("****");

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

return super.onOptionsItemSelected(item);
}

}



خوشحال میشوم دوستان بهم کمک کنند . هر نوع کمکی به هر نحوی ... نیاز دارم http://p30droid.com/images/smilies/blush.gif