如何用css,js和图像调用HTML文件? [英] How to call HTML file with css, js and images?

查看:149
本文介绍了如何用css,js和图像调用HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从服务器下载zip文件。在NSCachesDirectory我有HTML文件,我想知道如何运行HTML文件与他们的源文件,我已经粘贴了我的代码。请帮助我

  NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES); 
NSString * documentsPath = [[路径objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@/ Lesson-file / Lesson%d,[str_lsn_id integerValue]]];

NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@story_html5ofType:@htmlinDirectory:documentsPath]];
[lsn_play_webview loadRequest:[NSURLRequest requestWithURL:url]];


解决方案

这是一种适合您的方法。从上面说你有源文件 js css



这个问题可能是因为运行时间这些文件没有加载或编译器无法找到它们的位置。

例如:

 <! - 对于CSS文件html标签是 - > 
< link rel =stylesheethref =styles.css>

<! - 对于JS文件,html标签是 - >
< script type =text / javascriptsrc =exmample.js>< / script>

所以你必须手动提供这些文件的位置。



查看这里的标签 href src 。这些都是资源文件的位置,当你的html页面被加载到内存中时,这些资源文件将在运行时需要。



如果这些文件与目​​录相同无需提供网址

建议: 建议您在此提供文件位置网址。编辑您的html文件并为url标记添加标记。



示例代码:

 < HTML> < HEAD> 

< script type =text / javascriptsrc =#JS-FILE1#>< / script>
< link rel =stylesheethref =#CSS-FILE1#>

< / head> <身体GT; < /体>
< / html>

你可以看到我用标记#JS-FILE1# #CSS-FILE1#。现在我们将在加载html文件之前用实际的URL替换这些标记。

  NSString * path = [[NSBundle mainBundle] pathForResource:@ 文件名ofType:@html]; 
NSString * html = [[NSString alloc] initWithContentsOfFile:路径编码:NSUTF8StringEncoding error:nil];

//资源文件的位置
NSString * jsFilePath = [[NSBundle mainBundle] pathForResource:@exmampleofType:@js];

NSString * cssFilePath = [[NSBundle mainBundle] pathForResource:@stylesheetofType:@css];

html = [html stringByReplacingOccurrencesOfString:@#JS-FILE1#withString:jsFilePath];
html = [html stringByReplacingOccurrencesOfString:@#CSS-FILE1#withString:cssFilePath];

放置断点并调试输出字符串。 NSLog 这个字符串复制它并创建临时文件 - 在浏览器中打开它来检查所有资源是否加载?



如果没有,那么检查资源文件的URL是否正确。

I am downloading zip file from server. In NSCachesDirectory I have the html file and I would like to know how to run html file with their source files, I have pasted my code. please help me

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsPath =  [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"/Lesson-file/Lesson%d",[str_lsn_id integerValue]]];

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"story_html5" ofType:@"html" inDirectory:documentsPath]];
[lsn_play_webview loadRequest:[NSURLRequest requestWithURL:url]];

解决方案

This is an approach for you. From the context that you said that you have source files js and css.

Here issue may be because of on run time these files are not loaded or compiler not able to find their location.

For exmaple:

  <!--For CSS file html tag is-->
  <link rel="stylesheet" href="styles.css"> 

  <!--For JS file html tag is-->
  <script type="text/javascript" src="exmample.js"></script>

So you have to manually provide location of these files.

See the tags here href and src. These are location of resource files which will required on runtime when your html page is loaded in memory.

How ever if these files are in same directory than there is no need to provide url

Suggestion:

It is advisable that you provide file location url here. Edit your html file and add markers for the url tags.

Example code:

<html> <head>

        <script type="text/javascript" src="#JS-FILE1#"></script>
        <link rel="stylesheet" href="#CSS-FILE1#">

</head> <body> </body>
</html>

You can see that I have replaced files with markers #JS-FILE1# and #CSS-FILE1#. Now we will replace these markers with actual urls before loading html file.

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// Location of Resource files
NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"exmample" ofType:@"js"];

NSString *cssFilePath = [[NSBundle mainBundle] pathForResource:@"stylesheet" ofType:@"css"];

html = [html stringByReplacingOccurrencesOfString:@"#JS-FILE1#" withString:jsFilePath];
html = [html stringByReplacingOccurrencesOfString:@"#CSS-FILE1#" withString:cssFilePath];

Put here break point and debug the output string. NSLog this string copy it and create temporary file - open it in browser to cross check that all resources are loaded or not?

If not then check that url for resource files are correct.

这篇关于如何用css,js和图像调用HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆