1

尝试在使用 $.mobile.changepage() 加载的页面中加载外部 JS 文件时,我遇到了一个奇怪的问题。

以下是我的目录结构:

|_ /html
| |_ conflist.html
| |_ 新页面.html
|_ /常见
| |_ jquery-1.7.2.min.js
| |_ jquery.mobile-1.2.0.min.js
| |_ jquery.mobile-1.2.0.min.css
|_ /平台
    |_ dummy.js

以下是 conflist.html 和 newpage.html 的代码。

首先,conflist.html:

<!DOCTYPE HTML>
<html>
<head>
    <title>ConferenceToGo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <link rel="stylesheet" href="../common/jquery.mobile-1.2.0.min.css" />
    <script type="text/javascript" charset="utf-8" src="../common/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="../common/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" charset="utf-8">
        function changePage() {
            $.mobile.changePage("newpage.html", { transition: "slide" });
        }
    </script>
</head>
<body>
<div id="firstpage" data-role="page">
    <div data-role="content">
        <input type="button" onclick="changePage()" value="Change Page (JQM)" /><br />
    </div><!-- /content -->

</div><!-- /page -->

</body>
</html>

接下来,newpage.html:

<!DOCTYPE HTML>
<html>
<head>
    <title>ConferenceToGo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <link rel="stylesheet" href="../common/jquery.mobile-1.2.0.min.css" />
    <script type="text/javascript" charset="utf-8" src="../common/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="../common/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div id="newpage" data-role="page">
    <script type="text/javascript" charset="utf-8" src="../platform/dummy.js"></script>
    <script type="text/javascript" charset="utf-8">
        console.log("Inside test_newpage.html");
        var objDummy = new Dummy();
        objDummy.toString("test param");
    </script>
    <div data-role="content">
        <p>This is a new page !!</p>
    </div><!-- /content -->

</div><!-- /page -->

</body>
</html>

最后,dummy.js:

var Dummy = function () { };
Dummy.prototype.toString = function (param) {
    console.log("String representation of 'Dummy', got param: " + param);
};

console.log("Loaded dummy.js");

问题:

  • 如果我加载 conflist.html(直接使用 FF16.0.2 和 IE9,而不是通过网络服务器),然后单击按钮,我会收到一条错误消息:“ReferenceError: Dummy is not defined”。本质上,不读取 dummy.js。
  • If I move dummy.js to the same folder as the html files, and modify the path accordingly in newpage.html, things work fine and I get all the outputs.

Wondering, why do I see this behavior? Why isn't the external js file loaded when it is placed in another folder and the relative path is given?

Update: I checked and this also works if I put dummy.js in a subfolder inside the html folder. Just can't have it outside it seems, unless I am missing something.

4

3 回答 3

1

try to put your <script src="../platform/dummy.js"></script>into the head instead of the body... and: you do not need type="text/javascript" charset="utf-8"anymore...

<head>
    <title>ConferenceToGo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <link rel="stylesheet" href="../common/jquery.mobile-1.2.0.min.css" />
    <script src="../platform/dummy.js"></script>
    <script src="../common/jquery-1.7.2.min.js"></script>
    <script src="../common/jquery.mobile-1.2.0.min.js"></script>
</head>
于 2012-11-08T14:05:04.190 回答
0

Change your relative URLs to absolute ones. This will make sure you are referencing the correct file every time.

Your external page would look like this for example:

<!DOCTYPE HTML>
<html>
<head>
    <title>ConferenceToGo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <link rel="stylesheet" href="/base/common/jquery.mobile-1.2.0.min.css" />
    <script type="text/javascript" charset="utf-8" src="/base/common/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="/base/common/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div id="newpage" data-role="page">
    <script type="text/javascript" charset="utf-8" src="/base/platform/dummy.js"></script>
    <script type="text/javascript" charset="utf-8">
        console.log("Inside test_newpage.html");
        var objDummy = new Dummy();
        objDummy.toString("test param");
    </script>
    <div data-role="content">
        <p>This is a new page !!</p>
    </div><!-- /content -->

</div><!-- /page -->

</body>
</html>

Where /base/ is a top-level directory on your domain.

于 2012-11-08T20:27:40.803 回答
0

I think the reason might be the relative path of your js. I found that if you are using $.mobile.changePage(); only contents in the target page between the will be loaded, which means all the header content in target page will be ignored/not loaded. And when you reference js in the on your target page, the relative path should be base on the target html. and the "../" seems not working in this case.

于 2013-09-03T16:19:14.040 回答