简体   繁体   English

如何使用stringbyEvaluationJSS将Javascript插入$(document).ready(function()

[英]How to Inject Javascript with stringbyEvaluationJSS to $(document).ready(function()


hi 你好
i have a Problem,i want to Injet a var from nativ to my WebSolution. 我有一个问题,我想将一个变种从nativ注入我的WebSolution。
Cause i want to know, when a iDevice visit the page, some buttons have to be visible... 因为我想知道,当iDevice访问该页面时,某些按钮必须可见...
So i tried somethink like that:. 所以我尝试了这样的想法: On the index.html i have JQuery Script: 在index.html上,我有JQuery脚本:

$(document).ready(function(){ var htmlvar = varFromUiWebView; alert(htmlvar); }

At my xCode project: On the UIWebview Delegate: UiWebviewDidFinishload:, i have something like that: 在我的xCode项目中:在UIWebview委托:UiWebviewDidFinishload:上,我有类似的内容:

[browser stringByEvaluatingJavaScriptFromString:@"varFromUiWebView = 1;"];

So, the result is that the script crash, because 'varFromUiWebView' is undefined. 因此,结果是脚本崩溃,因为未定义'varFromUiWebView'。 How can i fix it? 我该如何解决?

Thank you in advance Konstantin 预先谢谢你康斯坦丁

at what point do you do the stringByEvaluatingJavaScriptFromString? 在什么时候您要执行stringByEvaluatingJavaScriptFromString?

My guess is that you have to wait for the page to be fully loaded before you try to run extra javascript ie you will need to have it in 我的猜测是,在尝试运行额外的javascript之前,您必须等待页面完全加载,即需要将其放入

- (void)webViewDidFinishLoad:(UIWebView *)theWebView

I realise this would mean that your js alert would probably appear before you get a chance to set the value you want to see but that's not the issue. 我意识到这意味着您的js警报可能会在您有机会设置要查看的值之前出现,但这不是问题。

hope this helps. 希望这可以帮助。

why not have all your iPhone buttons have a specific class (ie: iphone) the has a display:none css property and then in your app you can use: 为什么不让所有iPhone按钮都具有特定类(即iphone),并具有display:none css属性,然后在您的应用中可以使用:

- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
    NSString *insertRule = [NSString stringWithFormat:@"addCSSRule('iphone', 'display: block;')"];
    [theWebView stringByEvaluatingJavaScriptFromString:insertRule];
}

So at this time i think it isnt possible to inject a variable from : 因此,目前我认为无法从中注入变量:

[theWebView stringByEvaluatingJavaScriptFromString:@"var valueFromNativ = 1"];

to the DOM of the browser Website : 浏览器网站的DOM:
example : 例如:

$(document).ready(function(){
  var clientValue = valueFromNativ;
});


so a very easy workaround is to catch via userAgend the Device which visit your Page: 因此,一个非常简单的解决方法是通过userAgend捕获访问您网页的设备:

  $(document).ready(function() {
            var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android)/);
            if (isMobile == null) {
                $('#justTablets').hide();
            }
        });<br>

If now somebody visit with ipad my website a <div id="justTablets">Content ...</div> will shown, but if you are a simple desktop, laptop, it will be hidden ... see here http://jsfiddle.net/konstheinrich188/jASy8/ 如果现在有人使用ipad访问我的网站,则会显示<div id="justTablets">Content ...</div> ,但是如果您是台式机,笔记本电脑,它将被隐藏...请参阅此处http:// /jsfiddle.net/konstheinrich188/jASy8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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