简体   繁体   中英

Loading Javascript in webview

I am really stuck in a condition where I need to load a Html and Javascript code in webview. If is load it using an html file it works perfectly fine but not when I try to load it as a string.I need to load it as a string because I want to change the url in HTML dynamically.I think the problem is in the HTML string,can someone help me out.

  NSString *embedHTML = [NSString stringWithFormat:@"<HTML><HEAD><BODY>"
                           "<div id=\"fb-root\">sfd</div><script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = \"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=251520118193948\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>""<fb:comments-count href=http://abcd.com></fb:comments-count>"
                           "<BR></BODY></HTML>"];


    [webView loadHTMLString:embedHTML baseURL:nil];

Pretty easy: please push in u boundle index.html with HEADALEX, BODYALEX inside html, and replace by u strings

NSURL *indexURL = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSError *error = nil;

NSString *index = [NSString stringWithContentsOfURL:indexURL encoding:NSUTF8StringEncoding error:&error];

index = [index stringByReplacingOccurrencesOfString:@"HEADALEX" withString:htmlCodeHead];
index = [index stringByReplacingOccurrencesOfString:@"BODYALEX" withString:htmlCode];
[index writeToURL:indexURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) NSLog(@"PHONE CONFIG: write to file html error:%@",error);

[adsView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] isDirectory:NO]]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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