【问题标题】:I'm getting a blank screen when I try to load java script into a web view当我尝试将 java 脚本加载到 Web 视图中时出现空白屏幕
【发布时间】:2012-07-31 18:06:19
【问题描述】:

这是我的代码:

- (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 文件的内容。我这样做对吗?

【问题讨论】:

  • 您是否尝试在桌面浏览器中加载此代码?会发生什么?
  • 首先将alert('Done.');添加到script.js的底部。它正在设备上运行吗?也许将其移至&lt;head&gt;。下一个空的script.js 除了$('#button').click(function() { alert('clicked'); }); 之外的所有东西在点击/单击按钮时会被触发吗?然后您可以尝试.on('click', ...) 和下一个,尽管我不认为水龙头注册为tap 尝试.on('tap', ...)。还有一些想法。

标签: javascript html objective-c xcode uiwebview


【解决方案1】:

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

我不知道你的意思是你在源代码中看不到你的 js 代码,还是看不到它应该做的结果。如果您的意思是前者:使用 script 标签调用 script.js,不会将您的代码内联嵌入到您的 html 中。如果是后者:您的 js 代码应该使用 onload 属性或就绪侦听器调用。如果您提供您的 script.js,我可以提供更多帮助。

无论哪种方式,一个好的起点是验证您的 html 代码: http://validator.w3.org/

【讨论】:

  • 我看到你已经上传了你的代码。正如 Jim 提到的,您是否尝试过在常规浏览器中运行它?我的 jQuery 有点生疏,但我会尝试在第一行注释掉 "$(function () {" 和 "});" “$('#button').....” 行正上方的行,因为我认为这可能会阻止 .ready() 函数触发,但我可能弄错了。
  • 我试图将这些行注释掉,但我仍然遇到同样的问题。当我从桌面打开 index.html 文件时,它在浏览器中运行良好。但是当我使用上面的代码时,所有的输出都是测试
【解决方案2】:

问题是我从未将 .js 文件带到 Build Phases 下的 Copy Bundle Resources。我把文件移过来了,效果很好

【讨论】:

  • 如果 .html 文件和 .js 文件需要在应用程序的沙盒中而不是应用程序包中(我卡在这里。)?
【解决方案3】:

尝试转义 baseURL 中的反斜杠和空格。 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]]];

【讨论】:

    猜你喜欢
    • 2015-06-21
    • 2011-03-15
    • 2014-04-19
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2016-09-13
    • 2020-03-05
    • 1970-01-01
    相关资源
    最近更新 更多