在PhoneGap中在Windows Phone 7上包含本地JavaScript [英] Include local JavaScript within PhoneGap on Windows Phone 7

查看:180
本文介绍了在PhoneGap中在Windows Phone 7上包含本地JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PhoneGap应用程序设计为在多个移动平台上工作。我使用jQuery Mobile从互联网上的外部页面加载动态HTML内容。有问题的系统是Windows Phone 7.

I have a PhoneGap application designed to work on multiple mobile platforms. I'm loading a dynamic HTML content from an external page on the Internet using jQuery Mobile. The problematic system is Windows Phone 7.

这是我从外部页面得到的,脚本标签的URL已经替换为从手机加载,而不是从net以节省带宽:

This is what I get from the external page, with the URL of the script tag already replaced to load from the phone instead of from the net to save bandwidth:

<script type="text/javascript" charset="utf-8" src="x-wmapp1:/app/www/test.js"></script>

在我替换了 x-wmapp1时,在Android,iPhone甚至BlackBerry 部分(例如file:/// android_asset / www / on Android)。

This works fine on Android, iPhone and even BlackBerry when I replaced the x-wmapp1: part by a respective counterpart (e.g. file:///android_asset/www/ on Android). However, on Windows Phone 7 it doesn't seem to work at all.

当我尝试通过$ .getScript函数加载相同的URL时,它总是返回一个404

When I try to load the same URL via $.getScript function, it always returns a 404 eror, even if I try and load it with a relative path only.

有任何建议吗?

推荐答案

尝试在GapSourceDictionary.xml中添加您的js。

Try to add your js in the GapSourceDictionary.xml.

GapSourceDictionary.xml XML文件列出了所有HTML应用程序资源。当应用程序启动时,将读取此XML文件,并将列表中包含的所有文件添加到隔离的存储中,以便可以由WebBrowser控件提供。

The GapSourceDictionary.xml XML file lists all the HTML application resources. When the application starts, this XML file is read and all the files included in the list are added to the isolated storage so that it can be served by the WebBrowser control.

例如,您的GapSourceDictionary.xml应如下所示:

For example your GapSourceDictionary.xml should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <GapSourceDictionary>
        <FilePath Value="www\index.html"/>
        <FilePath Value="www\test.css"/>
        <FilePath Value="www\phonegap-1.3.0.js"/>
        <FilePath Value="www\js\custom.js"/>
    </GapSourceDictionary>

在HTML文件中使用相对路径指定外部脚本文件的URL:

In your HTML files use relative path to specify the URL of the external script file:

    <script type="text/javascript" src="./js/custom.js"></script>

已修改

我管理成功,使用以下过程动态加载新的本地JavaScript文件。测试在PhoneGap 2.0上成功。不幸的是,同样的测试在PhoneGap 1.8.1上失败了

I managed with success to dynamically load a new local JavaScript file using the following procedure. The test was successful on PhoneGap 2.0. Unfortunately the same test failed on PhoneGap 1.8.1

下面是动态加载JS的源代码:

Below is the source code which loads the JS dynamically:

function dynamicJSload(url)
{
    var script = document.createElement('script');
    script.type = "text/javascript";
    if (script.readyState)
    {
        script.onreadystatechange = function(){
            if (script.readyState == "complete" || script.readyState == "loaded"){
                script.onreadystatechange = null;
            }
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}



在我的页面中我调用了这样的函数:

In my page I called the function like this:

  dynamicJSload("js\\test.js");

我希望这可以帮助你。

这篇关于在PhoneGap中在Windows Phone 7上包含本地JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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