How i can call JS function from HTMLDocument OnShow Event?

Hi Support,
How i can call JavaSript function from HTMLDocument OnShow Event?

Here’s example of my code and event:

public static Stream ConvertTablesToImages(Stream xhtml, string publicationName)
        {
            try
            {
                using (var document = new HTMLDocument(xhtml, String.Empty))
                {
                    var tableContainers = document.GetElementsByTagName("table")
                        .Where(e => e.GetAttributeNode("class").Value == "table")
                        .Select(node => node.ParentElement)
                        .ToList();

                    if(tableContainers == null || !tableContainers.Any())
                    {
                        return xhtml;
                    }

                    int tablePosition = 0;

                    logger.Debug("Tables conversion started for - '" + publicationName + "'");

                    foreach (var tableContainer in tableContainers)
                    {
                        try
                        {
                            tablePosition++;

                            logger.Debug("Table conversion at [" + tablePosition + "] postion started");

                            var table = tableContainer.Children.First(elm => elm.LocalName == "table");

                            using (var streamProvider = new MemoryStreamProvider())
                            {
                                Stream imageStream;

                                using (var tableDocument = new HTMLDocument(WrapTable(table.OuterHTML), String.Empty))
                                {
                                    AttachCSS(tableDocument);

                                    Converter.ConvertHTML(tableDocument, saveOptions, streamProvider);

                                    imageStream = streamProvider.ImageStream;
                                }

                                var tableImage = document.CreateElement("img");
                                tableImage.SetAttribute("class", "image");
                                tableImage.SetAttribute("src", "data:image/png;base64," + ConvertToBase64String(imageStream) + "");

                                tableContainer.RemoveChild(table);
                                tableContainer.AppendChild(tableImage);
                            }

                            //logger.Debug("Table conversion at [" + tablePosition + "] postion finished");
                        }
                        catch(System.Exception ex)
                        {
                            logger.Error("Table conversion error for table at " + tablePosition + " position with error: \n", ex);
                            continue;
                        }
                    }
                    
                    logger.Debug("Tables conversion finished for - '" + publicationName + "'");

                    document.OnShow += (object sender, Event e) =>
                    {
                        //here i need call JavaScript function which included into HTMLDocument
                    };

                    MemoryOutputStorage memoryOutputStorage = new MemoryOutputStorage();

                    //save document to MemoryStream 
                    document.Save(memoryOutputStorage, HTMLSaveFormat.Original);

                    xhtml.Dispose();

                    return memoryOutputStorage.DocumentStream;
                }
            }
            catch (System.Exception ex)
            {
                logger.Error("Tables conversion error for publication - '" + publicationName + ": \n", ex);
                return xhtml;
            }
        }

@Kruzer

Aspose.HTML supports JavaScript processing while opening, rendering, or saving documents. For example, a document contains a script that changes the value of the “value” property of the “input” element and it executes at the time of the “onload” event. If you, after opening the document, accesses the property of the corresponding element, you will see that the script has been executed.

using var doc = new HTMLDocument("TestHTMLTemplateJS.htm");
var input = (HTMLInputElement)doc.GetElementById("javascriptTest");
var result = input.Value == "javascript works";

According to the HTML specification, for security reasons, setting this property does not change the value of the “value” attribute of the “input” element, so when saving this document, the result of the script is not visible. We can offer two solutions to this problem, you can change the script in the document so that it sets the attribute and not the property, for example like this:

function javascriptFunction() {
  document.getElementById('javascriptTest').setAttribute('value', 'javascript works');
}

In this case, after saving, the “value” attribute of the “input” element will contain the desired value. Or we can add an option that when the document is saved, the value of the “value” attribute of the “input” element will be taken based on the value of the “value” property.

Thank you for your answer

But i have one more question.

Can i call this function from “OnLoad” event?

    function javascriptFunction() {
      document.getElementById('javascriptTest').setAttribute('value', 'javascript works');
    }

Maybe somethink like it on C#?:

document.OnShow += (object sender, Event e) =>
                    {
                        javascriptFunction(); 
                    };

@Kruzer

Could you please share your sample HTML in .zip format with us? We will test the scenario in our environment and address it accordingly.