Project

General

Profile

How to use Javascript within a template.xml file without invoking doJavascript?

Added by Xinxi Wang almost 10 years ago

I want to put some JS in a template file and when the corresponding widget loads the JS also should load automatically. Is there anyway for doing that without invoking the doJavascript function in the C program?

For example, I have a widget that needs some animation coded in JS. Right now, I need to manually invoke doJavascript whenever the widget loads. Is there anyway to avoid the doJavascript function?

If doJavascript is not avoidable, where should I put this function? I need to remove the widget from the widget tree and then add it back later. So putting doJavascript in the constructor will not work. How do I know whether the widget is removed from the widget tree or not?

Thanks.


Replies (2)

RE: How to use Javascript within a template.xml file without invoking doJavascript? - Added by Georgiy Gluhoedov almost 10 years ago

Hello Xinxi

Use method virtual void Wt::WWidget::load()

[[[http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WWidget.html#a1ee433705523b2b79c4c3539e0852c92]]]

class Application : public Wt::WApplication
{
public:
    Application(const Wt::WEnvironment &env)
    {
        this->declareJavaScriptFunction("myJsFunction", "...");
    }
    //...
};


class MyWidget : public Wt::WTemplate
{
public:
    MyWidget//...
private:
    // call where widget load
    void load()
    {
        this->doJavaScript(wApp->javaScriptClass() + ".myJsFunction();");
    }
};

Br. Georgy

    (1-2/2)