Up vote 1 down vote favorite 2 share g+ share fb share tw.
I'm trying to "inject" a local css file into downloaded xhtml file. I found many examples of how to do it, but it just doesn't work for me... Here is my code: - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *path = NSBundle mainBundle bundlePath; NSString *cssPath = path stringByAppendingPathComponent:@"test. Css"; NSString *js = NSString stringWithFormat:@"var cssNode = document.
CreateElement('link');" "cssNode. Type = 'text/css';" "cssNode. Rel = 'stylesheet';" "cssNode.
Href = '%@';", cssPath; js = NSString stringWithFormat:@"%@document. GetElementsByTagName('head')0. AppendChild(cssNode);", js; //m_webview is a member m_webView stringByEvaluatingJavaScriptFromString:js; } What am I doing wrong here?
Thanks, iphone ios uiwebview link|improve this question edited Dec 20 '11 at 8:25 asked Dec 19 '11 at 14:50Gal495 100% accept rate.
I also had trouble doing this. I was trying to load an HTML file from a server and change the styling using a local CSS file. At first I used webView loadRequest:reqObj; And when it hit - (void)webViewDidFinishLoad:(UIWebView *)webView I was trying to push the CSS file as a child to 'head': - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *path = NSBundle mainBundle bundlePath; NSString *cssPath = path stringByAppendingPathComponent:@"style.
Css"; NSString *js = NSString stringWithFormat:@"var cssChild = document. CreateElement('link');" "cssChild = 'text/css';" "cssChild = 'stylesheet';" "cssChild = '%@';", cssPath; js = NSString stringWithFormat:@"%@ document. GetElementsByTagName('head')0.
AppendChild(cssChild);", js; webView stringByEvaluatingJavaScriptFromString:js; } So... it didn't work... then I tried NSString *path = NSBundle mainBundle bundlePath; NSURL *baseURL = NSURL fileURLWithPath:path; webView loadHTMLString:htmlString baseURL:baseURL; (I copied the HTML string into htmlString) and then, inside - (void)webViewDidFinishLoad:(UIWebView *)webView I injected the CSS as in the code above. And it worked! But... my HTML file is stored in a remote server and I didn't have the HTML string, so I used NSString* myFile = NSString stringWithFormat:@"http://blablabla.com/file.html"; NSString* myFileURLString = myFile stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding; NSData *myFileData = NSData alloc initWithContentsOfURL:NSURL URLWithString:myFileURLString; NSString* myFileHtml = NSString alloc initWithData:myFileData encoding:NSASCIIStringEncoding autorelease; To get the HTML.
Now, I have the raw HTML text inside ' myFileHtml '. I now use NSString *path = NSBundle mainBundle bundlePath; NSURL *baseURL = NSURL fileURLWithPath:path; webView loadHTMLString:myFileHtml baseURL:baseURL; And catching the response in ' webViewDidFinishLoad ', injecting my CSS file into it and it worked :) Maybe there's another, more elegant, solution to this problem, but this is what I came up with... Hope it helped.
Thanks! That did work. I just had to add 'myFileData release;' – Gal Dec 20 '11 at 14:06.
(Not tested in Xcode): - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *path = NSBundle mainBundle bundlePath; NSString *cssPath = path stringByAppendingPathComponent:@"test. Css"; NSString *js = NSString stringWithFormat:@"var cssNode = document. CreateElement('link');" "cssNode.
Type = 'text/css';" "cssNode. Rel = 'stylesheet';" "cssNode. Href = '%@';", cssPath; js = NSString stringWithFormat:@"%@document.
GetElementsByTagName('head')0. AppendChild(cssNode);", js; NSString *k = NSString stringWithFormat:@"var script = %@; %@", cssPath, js; //m_webview is a member m_webView stringByEvaluatingJavaScriptFromString:k; }.
Thanks, but it still looks the same. I wonder, how can I tell if 'stringByEvaluatingJavaScriptFromString' worked or failed? – Gal Dec 19 '11 at 15:41.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.