Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dynamically load HTML and insert into my web page with Dart?

Tags:

dart

How do I dynamically load a snippet of HTML and insert it into my web page? I am using Dart.

like image 723
Seth Ladd Avatar asked Jun 06 '12 03:06

Seth Ladd


People also ask

What is DOM in Dart?

Dart Masterclass Programming: iOS/Android Bible The way a document content is accessed and modified is called the Document Object Model, or DOM. The Objects are organized in a hierarchy. This hierarchical structure applies to the organization of objects in a Web document. Window − Top of the hierarchy.


1 Answers

Glad you asked! Using Dart for this task isn't much different than JavaScript, except you get typing, code completion, and a slick editing experience.

First, create the snippet.html:

<p>This is the snippet</p>

Next, create the application. Notice the use of XMLHttpRequest to request the snippet. Also, use new Element.html(string) to create a block of HTML from a string.

import 'dart:html';

void main() {
  var div = querySelector('#insert-here');
  HttpRequest.getString("snippet.html").then((resp) {
    div.append(new Element.html(resp));
  });
}

Finally, here's the host HTML page:

<!DOCTYPE html>

<html>
  <head>
    <title>dynamicdiv</title>
  </head>
  <body>
    <h1>dynamicdiv</h1>
    <div id="insert-here"></div>
    <script type="application/dart" src="dynamicdiv.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>
like image 94
Seth Ladd Avatar answered Sep 20 '22 07:09

Seth Ladd