Page 1 of 1

[SOLVED] TypeError: $thisType.htmlDocument is null

Posted: Thu Nov 23, 2017 7:00 am
by joaofig
Starting with the latest Beta 12.6, I am now seeing this error raised at function INTERNAL_HtmlDomManager_CreateDomFromStringAndAppendIt

Chrome reports it here:

Code: Select all

function INTERNAL_HtmlDomManager_CreateDomFromStringAndAppendIt (domAsString, parentRef, associatedUIElement) {
    if (true) {
      // Next line throws the error: Uncaught TypeError: Cannot read property 'createElement' of null
      var obj = $thisType.htmlDocument['createElement']("div");
      obj.innerHTML = domAsString;
      var obj2 = obj.firstChild;
      obj2.associatedUIElement = associatedUIElement;
      var result = obj2;
    } else {
      var text = $T1E()['CreateNew']();
      var uniqueIdentifier = JSIL.IgnoredMember("get_UniqueIdentifier()", $asm_CSharpXamlForHtml5.DotNetForHtml5.Core.INTERNAL_HtmlDomElementReference.$Cast(parentRef)).LValue;
      var javaScriptToExecute = $T1B()['Format']("\r\n// Create a temporary parent div to which we can write the innerHTML, then extract the contents:\r\nvar tempDiv = document.createElement(\"div\");\r\ntempDiv.innerHTML = \"{0}\";\r\nvar newElement = tempDiv.firstChild;\r\nnewElement.setAttribute(\"id\", \"{1}\");\r\nvar parentElement = document.getElementById(\"{2}\");\r\nparentElement.appendChild(newElement);\r\n", System.String.Replace(System.String.Replace(System.String.Replace(domAsString, "\"", "'"), "\r", ""), "\n", ""), text, uniqueIdentifier);
      JSIL.IgnoredMember("ExecuteJavaScript(javaScriptToExecute, commentForDebugging)", javaScriptToExecute, null).LValue;
      JSIL.IgnoredMember("INTERNAL_idsToUIElements", $thisType).LValue['Add'](text, associatedUIElement);
    return result;

Is there anything I can do about this?

Re: TypeError: $thisType.htmlDocument is null

Posted: Thu Nov 23, 2017 7:08 am
by JS-Support @Userware
Thanks for reporting this issue. We are going to investigate and fix it right away.


Re: TypeError: $thisType.htmlDocument is null

Posted: Fri Nov 24, 2017 3:35 am
by joaofig
This is somehow related to the FileOpenDialog plugin that you published. When I remove the control from the page, the error goes away. I did copy and paste your code into my project, no modifications added.

Hope this helps!

Re: TypeError: $thisType.htmlDocument is null

Posted: Fri Nov 24, 2017 3:55 am
by joaofig
I managed to work around this issue by removing the call to CSharpXamlForHtml5.DomManagement.SetHtmlRepresentation. Here's my final code:

Code: Select all

        public ControlForDisplayingTheFileOpenDialog()
            ResultKind = FileOpenDialog.ResultKind.Text; //Note: this is to set the default value of the property.

            //    "<input type='file'></input>");

            this.Loaded += ControlForDisplayingAFileOpenDialog_Loaded;

The handler was changed to:

Code: Select all

void ControlForDisplayingAFileOpenDialog_Loaded(object sender, RoutedEventArgs e)
    // Get the reference to the "input" element:
    var div = Interop.GetDiv(this);
    object inputElement = Interop.ExecuteJavaScript("(function(){var input = document.createElement('input'); input.type = 'file'; $0.innerHTML=''; $0.appendChild(input); return input; })()", div);

    Action<object> onFileOpened = (result) =>
        this.FileOpened?.Invoke(this, new FileOpenedEventArgs(result, this.ResultKind));
    string resultKindStr = _resultKindStr; // Note: this is here to "capture" the variable so that it can be used inside the "addEventListener" below.

    // Apply the "Filter" property to limit the choice of the user to the specified extensions:

    // Listen to the "change" property of the "input" element, and call the callback:
        $0.addEventListener(""change"", function(e) {
            if(!e) {
                e = window.event;
            var fullPath = $0.value;
            var filename = '';
            if (fullPath) {
                var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
                filename = fullPath.substring(startIndex);
                if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                    filename = filename.substring(1);
            var input =;
            var file = input.files[0];
            var reader = new FileReader();
            reader.onload = function() {
                var callback = $1;
                var result = reader.result;
                //if (file.type == 'text/plain')
            var resultKind = $3;
            if (resultKind == 'DataURL') {
            else {
            var isRunningInTheSimulator = $2;
            if (isRunningInTheSimulator) {
                alert(""The file open dialog is not supported in the Simulator. Please test in the browser instead."");
        });", inputElement, onFileOpened, CSHTML5.Interop.IsRunningInTheSimulator, resultKindStr);

Nothing else needs to be changed.

Re: TypeError: $thisType.htmlDocument is null

Posted: Thu Nov 30, 2017 12:55 am
by JS-Support @Userware

This issue has been fixed in Beta 12.7 and newer.