Javascript没有在iPhone中执行 [英] Javascript not executing in iPhone

查看:65
本文介绍了Javascript没有在iPhone中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用UIWebView从我的iPhone应用程序执行javascript代码。

I want to execute javascript code from my iPhone application using UIWebView.

但是只有html部分正在执行而javascript部分没有执行。它也没有显示错误。

But only html part is executing while javascript part is not executing. It's also not showing an error.

NSMutableString *htmlCode = [NSMutableString stringWithString:@"<!DOCTYPE HTML>"];


[htmlCode appendString:@"<html><head><tilte> sadaf </title>"];

[htmlCode appendString:@"<h1>My First Web Page</h1>"];


[ htmlCode appendString:@" <p id='demo'>This is a paragraph.</p>"];
[self.webView loadHTMLString:htmlCode baseURL:nil];


webView.delegate= self;

[webView stringByEvaluatingJavaScriptFromString:@"<script type='text/javascript'>document.getElementById('demo').innerHTML=Date();</script>   "];


推荐答案

检查委托是否设置正确。

Check if delegate has been set correctly.

- (void)viewDidLoad 
{
    [super viewDidLoad];

    //  init and create the UIWebView
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 88, 320, 400)]; 
    webView.autoresizesSubviews = YES;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [webView  setDelegate:self];//set the web view delegates for the web view to be itself
    NSMutableString *htmlCode = [NSMutableString stringWithString:@"<!DOCTYPE HTML>"];

    [htmlCode appendString:@"<h1>My First Web Page</h1>"]; 
    [htmlCode appendString:@" <p id='demo'>This is a paragraph.</p>"]; 
    [htmlCode appendString:@"</form> </body> </html> "]; 
    [self.webView loadHTMLString:htmlCode baseURL:nil];

    [self.view addSubview:webView];//add the web view to the content view
}

- (void) webViewDidFinishLoad:(UIWebView *)webView1
{
    NSLog(@"webViewDidFinishLoad");
    [webView1 stringByEvaluatingJavaScriptFromString:@"alert('hi');document.getElementById('demo').innerHTML=Date();"];
}

这可能有所帮助!

这篇关于Javascript没有在iPhone中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆