开发者

JQuery in html file from different baseUrl in iphone/ipad UIWebview

my app is creating html file from a string and inject it into my UIWebview using loadData.

my assets (images and js files) are downloaded from remote server into my device, and i give their baseUrl into the loadData method (documents/pics/assets).

in this directory i also have 2 files of jquery.

my problem is that the uiwebview can not use the jquery, i don't know why. my html finds the other assets in the same dir (images) but it seems it can't find the jquery.

when i try using it on the regular safari, it works. when inserting the js code into the html file, it works. but using the files localy not working on my ipad.

this is my code in app:

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]]];

and this is the code i'm trying to use in my html file:

<!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);开发者_JAVA技巧

                }

            });

        });

        function setEventDone(){

            window.location.href = 'myapp://loaded';

        }

    </script>

<script type="text/javascript">

    touchMove = function(event){

        // Prevent scrolling on this element

        event.preventDefault();

    }

</script>

the "assets/..." dir is in the documents/pics dir.

the code is not entering:

$('body').ready(function(){

thanks.


I had this problem today. You need to ensure that the JS files are copied into the bundle. By default, XCode tries to compile the JS rather than including them as resources for your app.

See also jQuery not loading on the iPhone

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜