2012-07-31 46 views
0

这里是我的代码:我得到了一个空白页面,当我尝试Java脚本加载到网络视图

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
NSError *error; 
NSURL *baseURL = [NSURL fileURLWithPath:path]; 
NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; 
NSLog(@"%@", html); 
[webview loadHTMLString:html baseURL:baseURL]; 
NSLog(@"error: %@", error); 
} 

这里是HTML的内容:

<html> 
<head> 
<script src="jquerymin.js"></script> 
<script src="highcharts.js"></script> 
<script src="exporting.js"></script> 
</head> 
<body> 
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

<script src="script.js"></script> 
Test 

<input type="button" id="button" /> 
</body> 

</html> 

的script.js:

$(function() { 
var chart; 
$(document).ready(function() { 
    chart = new Highcharts.Chart({ 
     chart: { 
      renderTo: 'container', 
      plotBackgroundColor: null, 
      plotBorderWidth: null, 
      plotShadow: false 
     }, 
     title: { 
      text: 'Therapist productivity by Region' 
     }, 
     tooltip: { 
      formatter: function() { 
       return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; 
      } 
     }, 
     plotOptions: { 
      pie: { 
       allowPointSelect: true, 
       cursor: 'pointer', 
       dataLabels: { 
        enabled: true, 
        color: '#000000', 
        connectorColor: '#000000', 
        formatter: function() { 
         return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; 
        } 
       } 
      } 
     }, 
     series: [{ 
      type: 'pie', 
      name: 'Therapist Productivity', 
      data: [ 
       ['South Carolina', 45.0], 
       ['Michigan',  26.8], 
       { 
        name: 'California', 
        y: 12.8, 
        sliced: true, 
        selected: true 
       }, 
       ['Florida', 8.5], 
       ['New York',  6.2], 
       ['Maine', 0.7] 
      ] 
     }] 
    }); 
}); 

}); 

$('#button').click(function() { 
$(chart.container).hide(); 
chart.series[0].remove(); 
$(chart.container).show(); 
}); 

当我运行我的代码,从HTML和“测试”按钮显示在屏幕上,但是被加载不是.js文件的内容。我做对了吗?

+0

您是否试图在桌面浏览器中加载此代码?怎么了? – Jim 2012-07-31 20:20:01

+0

首先将'alert('Done。');'添加到'script.js'的底部。它是否在设备上运行?也许把它移到''。接下来,除了'$('#button')之外的所有东西都是空的'script.js'。click(function(){alert('clicked');});'当按钮被点击/点击时,这会被触发吗?然后你可以尝试'.on('click',...)'和next,尽管我不认为点击注册为'tap'尝试'.on('tap',...)'。再多一些想法。 – 2012-08-01 00:49:58

回答

0

问题是我从来没有将.js文件复制到Build Phase下的Copy Bundle Resources。我移动了文件并完美运行

+0

Wat如果.html文件和.js文件需要在应用程序的沙盒中,而不是应用程序包(我卡在这里)? – 2012-10-30 12:41:49

0

这应该可能是一个评论,但我没有足够的代表,所以你会得到一个“答案”。

我不知道你的意思是你不能在源代码中看到你的js代码,或者你看不到它应该做的结果。如果您的意思是前者:使用脚本标记调用script.js,则不会将您的代码嵌入到HTML中。如果是后者:你的js代码应该使用onload属性或就绪监听器来调用。如果你提供你的script.js,我可以有更多的帮助。

无论哪种方式,一个良好的开端将是验证你的html代码: http://validator.w3.org/

+0

我看到你已经上传你的代码。正如吉姆所说,你有没有试过在普通的浏览器中运行它?我的jQuery有点生疏,但我会尝试在第一行注释“$(function(){”),而“});”在$('#button').....“行的上方,因为我认为这可能会阻止.ready()函数被触发,但我可能会误解。 – 2012-07-31 20:26:52

+0

我试图评论这些线,我仍然遇到同样的问题。当我从桌面打开index.html文件时,它在浏览器中正常工作。但是,当我使用上面的代码,所有输出是测试 – 2012-07-31 21:53:22

0

尝试逃离反斜杠和空格在您的基本URL。 Joe D'Andrea有一个很好的答案on a similar (but not quite duplicate) question。总结如下:

NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath] 
    stringByReplacingOccurrencesOfString:@"/" withString:@"//"] 
    stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
[webView loadHTMLString:markup baseURL:[NSURL URLWithString: 
    [NSString stringWithFormat:@"file:/%@//", resourcePath]]]; 
相关问题