Javascript 如何在ios uiwebview中为我的电子书阅读器使用xml内部的Java脚本?

Javascript 如何在ios uiwebview中为我的电子书阅读器使用xml内部的Java脚本?,javascript,html,css,xml,uiwebview,Javascript,Html,Css,Xml,Uiwebview,我可以用以下代码在HTML中加载JavaScripts标记: function loadjscssfile(filename, filetype){ if (filetype=="js"){ //if filename is a external JavaScript file var fileref = document.createElement('script'); fileref.src = filename; fileref.async = false; } else if (fil

我可以用以下代码在HTML中加载JavaScripts标记:

function loadjscssfile(filename, filetype){

if (filetype=="js"){ //if filename is a external JavaScript file
var fileref = document.createElement('script');
fileref.src = filename;
fileref.async = false;
}
else if (filetype=="css"){ //if filename is an external CSS file
    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined"){
    document.head.appendChild(fileref);
      }
}
loadjscssfile("jquery.min.js", "js") //dynamically load and add this .js file
loadjscssfile("annotator.min.css", "css") ////dynamically load and add this .css file
但我想在epub阅读器中使用它,它可以分解为xml/html。我希望在ios uiwebview中打开电子书阅读器时执行相同的js

示例xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
      <title>CHAPTER 13 - DR. SEWARD'S DIARY&#8212;cont. | Dracula</title>
      <link rel="stylesheet" href="css/book.css" type="text/css"/>
      <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
      <meta name="EPB-UUID" content="01D8803E-6BF7-1014-BF2E-F46A8E11922F"/>
   </head>
   <body>
      <div class="body">
         <div class="chapter">
            <h3 class="chapter-title">CHAPTER 1</h3>
            <h4 class="chapter-subtitle">DR. DIARY&#8212;cont.</h4>
            <p>"She makes a very beautiful corpse, sir. It's quite a privilege to attend on her. It's not too much to say that she will do credit to our establishment!"</p>
        </div>
      </div>
   </body>
</html>

第13章-苏厄德博士的日记—;续|德古拉
第一章
日记博士—;续。
“她是一具非常漂亮的尸体,先生。照顾她是一种莫大的荣幸。说她会为我们的机构增光添彩也不过分!”


您可以在xml中使用JavaScript,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<content>
  <data>
    <![CDATA[
      <script type="javascript">
          alert("hello");
          <script src="~/Scripts/jquery-1.8.2.js"></script>
      </script>
  ]]>
  </data>
</content>

警惕(“你好”);
]]>

您只需为JS和CSS添加这两个函数:

    - (void) addJSScripts
{
    NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"jquery.min" ofType:@"js"];
    NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];
    NSString *javascriptCode1 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

jsFilePath = [[NSBundle mainBundle] pathForResource:@"annotator.offline.min" ofType:@"js"];
jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode2 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

jsFilePath = [[NSBundle mainBundle] pathForResource:@"offlinejs" ofType:@"js"];
jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode3 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

jsFilePath = [[NSBundle mainBundle] pathForResource:@"startjs" ofType:@"js"];
jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode4 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

[_webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@\n%@\n%@\n%@",javascriptCode1, javascriptCode2, javascriptCode3, javascriptCode4]];
}


}

我希望外部JS文件包含在xml文件中。是的,您可以在.Modified节中给出JS文件的路径。不确定您是如何解析xml文件的,但我认为您需要获取元素的内容并将其加载到webview。。希望这能帮上忙,没用!为了您的参考,我为您附上了我的示例xml!你能测试一下它是否对你方有效吗?你能在部分内添加标签并尝试一下吗。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:@"annotator.min" ofType:@"css"];
NSString *javascriptString = @"var link = document.createElement('link'); link.href = '%@'; link.rel = 'stylesheet'; document.head.appendChild(link)";
NSString *javascriptWithPathString = [NSString stringWithFormat:javascriptString, path];
[_webview stringByEvaluatingJavaScriptFromString:javascriptWithPathString];

path = [[NSBundle mainBundle] pathForResource:@"annotator.touch" ofType:@"css"];
javascriptString = @"var link = document.createElement('link'); link.href = '%@'; link.rel = 'stylesheet'; document.head.appendChild(link)";
javascriptWithPathString = [NSString stringWithFormat:javascriptString, path];
    [_webview stringByEvaluatingJavaScriptFromString:javascriptWithPathString];
[self addJSScripts];