Official way to embed the native JavaScript code?

164 views
Skip to first unread message

Tom

unread,
May 4, 2012, 6:25:10 AM5/4/12
to General Dart Discussion
DartEditor complains "Native keyword can be used only in corelib,"
when embedding JavaScript code in Dart (with the native statement). It
looks like it is an internal mechanism that only the Dart team can
use. However, without it, how do I integrate the 3rd party JS
libraries, such as Google Maps? Is there any official approach? Thanks.

John Evans

unread,
May 4, 2012, 8:45:04 AM5/4/12
to mi...@dartlang.org
I do it this way.

  /**
* Injects javascript into the DOM, and optionally removes it after the script has run. */
  static void inject(String javascript, [bool removeAfter = false]){
    var s = Dom.createByTag("script");
    s.attributes["type"] = "text/javascript";
    s.text = javascript;

    document.body.nodes.add(s);

    if (removeAfter != null && removeAfter)
      s.remove();
  }

Two-way integration with JS is another matter, you can call JS libs using the method above, but getting any return values back into Dart isn't easily accomplished.

Maxi

unread,
May 5, 2012, 7:56:12 AM5/5/12
to General Dart Discussion
domisolates are supposed to fix this in the future, but I'm also
waiting for them.
Reply all
Reply to author
Forward
0 new messages