Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript在HTML文件中插入视口元_Javascript_Objective C_Uiwebview - Fatal编程技术网

使用Javascript在HTML文件中插入视口元

使用Javascript在HTML文件中插入视口元,javascript,objective-c,uiwebview,Javascript,Objective C,Uiwebview,在我的示例中,我想将Javascript注入UiWebView RSS\u Dettaglio\u Webv的内容中。为什么我不能插入视口图元 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]]; [RSS_Dettaglio_Webv loadRequest:request]; [RSS_Dettaglio_Webv setBackgroundColor:[UIColor w

在我的示例中,我想将Javascript注入
UiWebView RSS\u Dettaglio\u Webv
的内容中。为什么我不能插入视口图元

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
[RSS_Dettaglio_Webv loadRequest:request];
[RSS_Dettaglio_Webv setBackgroundColor:[UIColor whiteColor]];

NSString *javaScript = @"var viewPortTag=document.createElement('meta');\
viewPortTag.id=""viewport"";\
viewPortTag.name = ""viewport"";\
viewPortTag.content = ""width=320"";\
document.getElementsByTagName('head')[0].appendChild(viewPortTag);";

[RSS_Dettaglio_Webv stringByEvaluatingJavaScriptFromString:javaScript];

meta
标签在加载文档时会被解析,否则它们将被忽略。请注意,我使用纯js检测视口大小,并将html页面调整为该大小

更新

如果必须使用meta标记,则可以将HTML作为字符串而不是文件加载,在加载字符串之前修改字符串。。。类似这样(未测试的代码)-将“宽度:设备宽度”放入
meta
标记中,然后搜索并替换它:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm"];  
if (filePath) {  
    NSString *firstHTML = [NSString stringWithContentsOfFile:filePath];  
    if (firstHTML) {
        NSString *html;
        html = [firstHTML stringByReplacingOccurancesOfString:@"device-width" withString:@"320"];  
        [self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
    }  
}  

因此,在页面显示在UiWebview上之前,我没有机会将“viewport”注入HTML?只能通过修改HTML(例如,将其加载到字符串中,修改它,将字符串加载到UiWebview而不是URL),或者使用
document.write()
中的
。您能告诉我在加载到web视图之前编辑HTML的过程吗?