Project

General

Profile

HTML entered through WTemplate::setText not being added to page - Why?

Added by Osman Zakir over 4 years ago

As the title says, the HTML I have in my WTemplate::setText isn't appearing anywhere in the <body> element. So is there anyone who can help me? Current code: https://gist.github.com/DragonOsman/5475d9f9b6fc8c379eba77a504b71bcf and the JS file: https://gist.github.com/DragonOsman/c6e8fb15343544e662f474c5a526d1c2 . Hopefully somebody on here can help me. Thanks in advance.


Replies (10)

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Osman Zakir over 4 years ago

WTemplate::setTemplateText, I mean. Sorry about the mistake.

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Wim Dumon over 4 years ago

Osman,

Most likely the dom elements are inserted, but the script tags are not executed. You cannot execute scripts this way since a browser will not execute scripts that were inserted by innerHtml calls. You must use require() and doJavaScript().

Best regards,

Wim.

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Osman Zakir over 4 years ago

I used "Inspect Element" on the page and saw that the HTML I'd tried to enter wasn't there at all. That's why I'm asking.

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Osman Zakir over 4 years ago

Making this post to add: I commented out the code in the WApplication-derived class constructor and created an actual file called index.html which I placed in my docroot. Then I navigated to http://localhost:8080/index.html (I want to know if I can make it so that it's possible to get to it without me having to explicitly type "/index.html" there), and saw in the Wt output that successfully gets scripts.js with a "200 OK", but is not even trying to get mystyles.css. I also have an error in the browser's console that I heard is apparently an execution-stopping one, and that's why the page is completely blank right now (no map). But it's an error related to the code where I'm trying to load my Node module, so I don't get how an execution-stopping error there could making the map itself not appear. It's a Node module for only country to currency mapping, after all.

By the way, is there a way for me to substitute the Google API Key in my HTML file with a variable from the wt_config.xml file whose value is the actual API key? Can WTemplate be used to do this or do I have to do something else for it?

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Roel Standaert over 4 years ago

When I test it the HTML is definitely there. I think that the browser blocks <script> tags inserted by JavaScript. You could use require() or doJavaScript instead.

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Osman Zakir over 4 years ago

That doesn't make sense to me. I have JavaScript turned on, and the JavaScript code worked perfectly in the Boost.Beast version of this same app as well. I'll try it with WTemplate again and get back to you.

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Osman Zakir over 4 years ago

I just checked again with WTemplate, and found that when I look under the "Element" tab in MS Edge's Developer Tools, the HTML I put in through WTemplate::setTemplateText isn't in the

element at all. Same for Google Chrome. And when I put "/index.html" in there, I see that it successfully gets the script file mentioned in

tags with the "src" attribute. So the browser is accepting the

tag.

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by lm at over 4 years ago

Here's what I did:

#include "Wt/WApplication.h"
#include "Wt/WDialog.h"
#include "Wt/WPushButton.h"

namespace nn {
    struct application : Wt::WApplication {
        application() = delete;
        application(application const&) = delete;
        auto operator =(application const&) -> application& = delete;
        application(application&&) = delete;
        auto operator =(application&&) -> application& = delete;
        inline application(Wt::WEnvironment const& e);
    };

    application::application(Wt::WEnvironment const& e)
        : Wt::WApplication{e} {
            require("/javascript.js");
    }
}

auto main(int c, char** v) -> int {
    Wt::WRun(c, v, [&](Wt::WEnvironment const& e){
            return std::make_unique<nn::application>(e);
    });
    return 0;
}

"javascript.js" has

window.onload = function() { console.log("hello, world"); };

and I run using

./osman --docroot '.;/javascript.js' --http-address 0.0.0.0 --http-port 9090

RE: HTML entered through WTemplate::setText not being added to page - Why? - Added by Osman Zakir over 4 years ago

I have this HTML: https://gist.github.com/DragonOsman/506bd2bae1dfe4c03a4bbe2a6d830324 , and I want to reproduce it as is in Wt including the attributes. Even if none else, I at least want to keep the data-main one since it's needed for Require.js to be able to tell where the main script is. And I'm using Require.js to load the country to currency mapping Node.js module I'm using (with require() in client-side Javascript).

    (1-10/10)