如何使用 stringbyEvaluationJSS 将 Javascript 注入到 $(document).read
How to Inject Javascript with stringbyEvaluationJSS to $(document).ready(function()
嗨,
我有一个问题,我想将一个变量从 nativ 喷射到我的网络解决方案。
因为我想知道,当iDevice访问该页面时,某些按钮必须可见...
所以我尝试了这样的想法:。在索引上.html我有 JQuery 脚本:
$(document).ready(function(){ var htmlvar = varFromUiWebView; alert(htmlvar); }
在我的xCode项目中:在UIWebview Delegate:UiWebviewDidFinishload:上,我有这样的东西:
[browser stringByEvaluatingJavaScriptFromString:@"varFromUiWebView = 1;"];
因此,结果是脚本崩溃,因为"varFromUiWebView"未定义。我该如何解决它?
提前谢谢你康斯坦丁
你在什么时候做 stringByEvaluatingJavaScriptFromString?
我的猜测是,在尝试运行额外的javascript之前,您必须等待页面完全加载,即您需要将其放入
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
我意识到这意味着您的 js 警报可能会在您有机会设置要查看的值之前出现,但这不是问题所在。
希望这有帮助。
为什么不让你所有的iPhone按钮都有一个特定的类(即:iPhone)有一个display:none css属性,然后在你的应用程序中你可以使用:
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSString *insertRule = [NSString stringWithFormat:@"addCSSRule('iphone', 'display: block;')"];
[theWebView stringByEvaluatingJavaScriptFromString:insertRule];
}
所以在这个时候我认为不可能从以下位置注入变量:[theWebView stringByEvaluatingJavaScriptFromString:@"var valueFromNativ = 1"];
到浏览器网站的DOM : 示例:
$(document).ready(function(){
var clientValue = valueFromNativ;
});
因此,一个非常简单的解决方法是通过userAgend捕获访问您页面的设备:
$(document).ready(function() {
var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android)/);
if (isMobile == null) {
$('#justTablets').hide();
}
});<br>
如果现在有人访问ipad我的网站,将显示<div id="justTablets">Content ...</div>
,但如果你是一个简单的台式机,笔记本电脑,它将被隐藏......看这里http://jsfiddle.net/konstheinrich188/jASy8/
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- Bootstrap javascript at very end and jQuery $(document).read
- 如何使用 stringbyEvaluationJSS 将 Javascript 注入到 $(document).read