Haxe js api was incomplete: js.html.Element has no field src

js.html.Element has no field src
var tag=document.createElement("script");

tag.src = "https://www.youtube.com/iframe_api";//js work
import js.Browser;

class YoutubePlayer {
	public function new() {
		var tag = Browser.document.createElement('script');
		tag.src = "https://www.youtube.com/iframe_api";//error here.
	}

	public static function main() {
        var p=new YoutubePlayer();

        trace(p);
    }
}

Because js.html.Element does not have a src field. ScriptElement does.

Try that:

var tag:js.html.ScriptElement = cast js.Browser.document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api"; // works fine

Try that in js to see what you’re getting:

console.dir(document.createElement('script').__proto__);
// HTMLScriptElement

oh…I see. thanks .

You can also use document.createScriptElement and simular methods instead of cast (exists only in Haxe api). Cast only required for getElement stuff.

1 Like