[英]JQuery in html file from different baseUrl in iphone/ipad UIWebview
我的應用程序是根據字符串創建html文件,然后使用loadData將其注入到UIWebview中。
我的資產(圖像和js文件)從遠程服務器下載到我的設備中,我將其baseUrl放入loadData方法(文檔/圖片/資產)中。
在這個目錄中,我也有2個jquery文件。
我的問題是uiwebview不能使用jquery,我不知道為什么。 我的html在相同的目錄(圖像)中找到其他資產,但似乎找不到jquery。
當我嘗試在常規野生動物園中使用它時,它可以工作。 將js代碼插入html文件時,它可以工作。 但是使用本地文件無法在我的iPad上使用。
這是我在應用程序中的代碼:
NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath = [NSString stringWithFormat:@"%@/pics", [docDir objectAtIndex:0]];
NSString *imagePath = [NSString stringWithFormat:@"%@/pics", [docDir objectAtIndex:0]];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSData *htmlDataPortrait = [_stringFromData dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES];
[_portraitWebview loadData:htmlDataPortrait MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];
這是我要在我的html文件中使用的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 1024px;
margin-bottom: 606px;
background-repeat: no-repeat;
background-color: transparent;
}
</style>
<script type="text/javascript" src="assets/jquery.min.js"></script>
<script type="text/javascript" src="assets/func.js"></script>
<script>
$('body').ready(function(){
reOrderArtWithImgOpt('1');
$('div#fullpage').css('visibility', 'visible');
imgNum = $('img[width!=0]').length;
imgIndex = 0;
$('img').load(function(){
++imgIndex;
if (imgIndex == imgNum) {
setTimeout("setEventDone()", 50);
}
});
});
function setEventDone(){
window.location.href = 'myapp://loaded';
}
</script>
<script type="text/javascript">
touchMove = function(event){
// Prevent scrolling on this element
event.preventDefault();
}
</script>
“資產/ ...”目錄位於文檔/圖片目錄中。
代碼未輸入:
$('body').ready(function(){
謝謝。
我今天有這個問題。 您需要確保將JS文件復制到分發包中。 默認情況下,XCode嘗試編譯JS,而不是將其作為應用程序的資源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.