簡體   English   中英

將參數從目標傳遞到javascript

[英]passing parameters from objective to javascript

我想從Objective-C向javascript發送一些NSString值。 這是代碼:

Javascript.html

<!DOCTYPE html>
<html>
    <head>
        <title>Sample Page that Writes Out an HTML Addition Table</title>
        <script src=“doc.js”></script>
    </head>
    <body>
    </body>
</html>

doc.js

function call(str){
    document.write('<table border="1" cellspacing="1" cellpadding="5">')
    for(i = 0; i < 4; i++){
        document.write('<tr>')
        for (j = 0; j < 3; j++){
            document.write('<td>'+str+'<img src= '+str+'> </img></td>')
        }
        document.write('</tr>')
    }
    document.write('</table>')
}

.m文件

-(IBAction) show:(id) sender{

    NSString *htmlpath = [[NSBundle mainBundle] pathForResource:@"Javascript" ofType:@"html"];
    NSString *html =[NSString stringWithContentsOfFile:htmlpath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",[[NSBundle mainBundle] bundlePath]] ];
    [[webView mainFrame] loadHTMLString:html baseURL:baseURl];

    NSString *jsTable=[NSString stringWithFormat:@"call('file:///Users/developer/Library/Billing/Barcodes/CO_BAT01_14.png')"];
    NSLog(@"jsTable = %@",jsTable);
    [webView stringByEvaluatingJavaScriptFromString:jsTable];
    [jsTable release];
}

我想從我的應用程序將圖像路徑發送到.js文件。 任何人都可以幫助我解決我的問題!!! 提前致謝。

在執行javascript之前,您應該等待HTML加載。 為您的WebView設置frameLoadDelegate並實現此方法:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    NSString *jsTable=[NSString stringWithFormat:@"call('file:///Users/developer/Library/Billing/Barcodes/CO_BAT01_14.png')"];
    NSLog(@"jsTable = %@",jsTable);
    [sender stringByEvaluatingJavaScriptFromString:jsTable];
    [jsTable release];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM