Project

General

Profile

Using JS with Wt

Added by dea lock over 6 years ago

Hello, I want to use 2 JS functionalities in my project

1. scrolling to the end of WText, text is appended by separate thread using postall

I've tried (and few others from stackoverflow) : app~~doJavaScript("var txtDiv = document.getElementById(\"\" + vTxt~~>id() + "\"); txtDiv.scrollTo = txtDiv.scrollHeight;\");

2. colorize syntax of WText.text() - with 4 ex. prism ,highlight or shjs

I've added require("highlight.pack.js"); useStyleSheet("default.css"); to MyWApplication class

vContainer->WWebWidget::setHtmlTagName("pre");

vTxt->Wt::WWebWidget::setHtmlTagName("code");

vTxt->Wt::WWebWidget::doJavaScript("hljs.initHighlightingOnLoad();");

nothing works, plz help :(


Replies (5)

RE: Using JS with Wt - Added by lm at over 6 years ago

Normal debugging procedures apply here. Of course, crazy things could be happening, but let's rule out simple things first. For instance, add a console.log(txtDiv); or debugger; after getting the element from the document? How does the webpage source look? Etc.

RE: Using JS with Wt - Added by Wim Dumon over 6 years ago

I would suggest to use a WTemplate. Here's the general idea:

  template = new WTemplate("

<pre class="force-pandoc-to-ouput-fenced-code-block"><code class=\"html\">${text}</code>&lt;/pre>", parent);
  template->bindText("text", myCode);
  template->doJavaScript("hljs.highlightBlock(" + template->jsRef() + ");");

Wim.

RE: Using JS with Wt - Added by dea lock over 6 years ago

1. This works : vTxt.scrollIntoView(false); Seting value to scrollTop doesn't (scrollTo is window, not element method).

  1. I don't get any errors , js and css files load ok, but doing 4ex. app->doJavaScript("sh_highlightDocument();");
    befor/after vTxt->setText(someStringToColorize) does nothing

RE: Using JS with Wt - Added by lm at over 6 years ago

If you have code like this:

vTxt->setText("Hello, world!");
app->doJavaScript("sh_highlightDocument();");

I'm wondering if the javascript is run before the text is actually updated client side? Again, normal debugging procedures: in the doJavaScript code, print out the text of the input that is about to be highlighted (or its length) to the console?

RE: Using JS with Wt - Added by dea lock over 6 years ago

  • WTemplate works only with TextFormat::XHTML , I need Plain :/
    I've added app~~doJavaScript("console.log((document.getElementById(\"\" +vTxt~~>id()+ "\")).textContent);");
    before/after appending "ABC\" to "123", and before/after vTxt->setText()
    all 4 return "123ABC" ??
    (1-5/5)