3

我正在尝试在 Firefox (v22.0) 中运行我的 Dart 应用程序。这是应用程序的主页:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>My 1st Dart App</title>
        <link rel="stylesheet" href="assets/myapp/myapp/myapp.css">
    </head>
    <body>
        <h2>Push the button!</h2>

        <div id="sample_container_id">
            <input type="button" id="someButton" value="Some Button!" />
        </div>

        <script type="application/dart" src="myapp.dart"></script>
        <script src="packages/browser/dart.js"></script>
    </body>
</html>

当我按原样运行时,我的应用程序运行良好,并且完全按照我的意愿运行。但是,如果我去掉第一个<script/>标签 ( <script type="application/dart" src="myapp.dart"></script>),则 Dart 代码不会在运行时执行。例如,我在 HTML 按钮上配置了一个点击处理程序,如下所示:

void main() {
    querySelector("#someButton").onClick.listen((e) => window.alert("Hello!"));
}

如果我删除第一个<script/>标签,那么当我点击时someButton什么也不会发生。

(1) 为什么删除第一个<script/>标签会“杀死” Dart 代码?pub build用来生成交叉编译的 JavaScript,那么为什么 Firefox 应该关心我的 Dart 源文件(因为 FF 22.0 本身不支持 Dart)?

(2) Dart 有没有推荐的<DOCTYPE>声明,比如transitional等?

4

4 回答 4

4

Short answer: Look into dart.js to find out what's going on.

Long answer: Dart.js will look for the script: application/dart, if it's running on a browser with DartVM (i.e.: Dartium) it will launch that. Otherwise it will take the script file myapp.dart and change the file to be myapp.dart.js and run that. By removing that script tag, you are removing your dart script which dart.js looks for and as a result your javascript compiled version is never inserted into your document.

Regarding DOCTYPE none is particularly recommended, however since Dart is generally designed for the latest versions of browser and in fact uses HTML5 tags where appropriate it would probably be best to use the HTML5 DOCTYPE as you have in your example.

于 2013-12-23T15:58:05.363 回答
3

1) Take a look at what packages/browser/dart.js does.

It first checks if the browser supports Dart, then if the browser does not support Dart, it looks for files with type application/dart and remaps them to point to the correct JavaScript file.

Since you removed your Dart script, dart.js doesn't find it and never remaps it to point to the JavaScript file.

2) Everything I've seen uses the HTML5 doctype: <!DOCTYPE html> but I don't think there is an official guideline.

于 2013-12-23T15:59:04.247 回答
2
  1. packages/browser/dart.js checks if the DartVM is available, if not it replaces <script type="application/dart" src="xxx.dart"></script> with <script src="xxx.dart.js"></script>. Assuming you have run dart2js you can do that job manually by replacing the 2 script tags with only <script src="myapp.dart.js"></script>
  2. there's no recommandation.
于 2013-12-23T15:56:26.227 回答
1

1) Firefox doesn't care about the Dart source file but pub build does. If your HTML file doesn't contain a Dart script pub build can copy the file without any processing otherwise pub build adds JavaScript code that executes instead of the Dart script when the browser doesn't support Dart.

2) Your doctype is the correct HTML5 doctype and Dart works fine with it.

于 2013-12-23T15:58:25.023 回答