Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mobile safari:frame.src vs window.location_Javascript_Ios_Webview_Webkit - Fatal编程技术网

Javascript Mobile safari:frame.src vs window.location

Javascript Mobile safari:frame.src vs window.location,javascript,ios,webview,webkit,Javascript,Ios,Webview,Webkit,嗨,快速提问 从webview调用本机的最佳方式是什么。iframe还是window.location 例如: 或: 顺便说一句,在嵌入式webview中更改src似乎不起作用。正如stack上的其他文章所揭示的,iframe似乎更适合我的情况。我看到window.location的问题是,如果您有多个顺序调用,浏览器将忽略一些调用。在使用iframe时,实际上可以为每个调用创建多个iframe。我也会在延迟之后删除iframe,这样我就不会发现自己有大量的空iframe 以下是我使用的函数:

嗨,快速提问

从webview调用本机的最佳方式是什么。iframe还是window.location

例如:

或:


顺便说一句,在嵌入式webview中更改src似乎不起作用。正如stack上的其他文章所揭示的,iframe似乎更适合我的情况。我看到window.location的问题是,如果您有多个顺序调用,浏览器将忽略一些调用。在使用iframe时,实际上可以为每个调用创建多个iframe。我也会在延迟之后删除iframe,这样我就不会发现自己有大量的空iframe

以下是我使用的函数:

function _callNative(url){
    var _frame = document.createElement('iframe');
    _frame.width=0; _frame.height=0;_frame.frameBorder=0;
    document.body.appendChild(_frame);
    if (url.indexOf('?') >= 0){
        url = url + "&cb=";
    }else{
        url = url + "?cb=";
    }
    _frame.src = url + Math.round(Math.random()*1e16);
    // Remove the iframe
    setTimeout(function(){document.body.removeChild(_frame);}, 2000);
}
例如:


以下是考虑到性能的不同备选方案的概述:

基本上,如果您支持iOS 8或更低版本,那么最好使用location.replace


如果您支持iOS 9及更高版本,并且差异最小,您可以选择您更喜欢location.replace还是WKScriptMessageHandler。

嘿,感谢您的回复:当我说iframe时,我引用了此iframe的单个实例。我看你用了很多。你有关于为什么这样更好的信息吗?您还说,我看到window.location的问题是,如果您有多个连续调用,浏览器将忽略一些调用。你有链接吗谢谢这都是基于我自己的经验,对不起,我没有链接。最好是自己尝试,使用一个iframe、window.location和多个iframe进行尝试。所有方法都应该可以正常工作,但我需要按顺序执行几个调用,它只是注册了一个。有多个iframe的方法是唯一一个对我起作用的方法。你应该试试看。如果我看不到,我不相信。该测试台使用各种方法(包括window.locaion)来回执行8000次成功调用。我想看看这方面的证据。我真的不喜欢过度设计。这篇文章已经有三年多的历史了。我认为这个方法没有那么复杂,我不会把它称为工程。在过去的几年里,我没有做太多的iOS开发,所以我不能说我3年前看到的限制是否仍然有效。我自己喜欢稳妥地犯错。我发现在iFrame上打开这些URL也更优雅。但是做你自己的研究我来这里不是为了向你证明什么,做你认为最好的事。如果我说得有点难的话,我很抱歉。顺便说一句,你是对的。这家伙可以确认,在事件循环的同一旋转中反复设置location.href只会导致最终导航。使用iframe是避免这种情况的一种方法。
window.location = custom + "://" + custom;
function _callNative(url){
    var _frame = document.createElement('iframe');
    _frame.width=0; _frame.height=0;_frame.frameBorder=0;
    document.body.appendChild(_frame);
    if (url.indexOf('?') >= 0){
        url = url + "&cb=";
    }else{
        url = url + "?cb=";
    }
    _frame.src = url + Math.round(Math.random()*1e16);
    // Remove the iframe
    setTimeout(function(){document.body.removeChild(_frame);}, 2000);
}
_callNative('native://doSomethingNative()');
_callNative('native://doSomethingElse')