Discussion:
[Wt-interest] How to call javascript function with parameters from C++?
ZhiTao Chen
2009-08-06 03:35:02 UTC
Permalink
hi all,Is it possible with Wt to call a client javascript function with
parameters?
ex. There's a js function: function showMsg(msg) { alert(msg); }
How to call it just like alert('hello'); in C++ code?
Zhimin Song
2009-08-06 06:21:06 UTC
Permalink
hi, ZhiTao
the following is abstracted from the Library overview of Wt doc, try them,
I have never used it:-)

When the requirements for stateless slot learning cannot be met you will
have to resort to writing JavaScript manually. Wt provides a number of
mechanisms to integrate JavaScript code with C++:


- Using JSlot<file:///home/cx/software/wt-2.99.1/doc/reference/html/classWt_1_1JSlot.html>,
you can specify the JavaScript for a slot, when connected to an
EventSignal<file:///home/cx/software/wt-2.99.1/doc/reference/html/classWt_1_1EventSignal.html>.

- Using JSignal<file:///home/cx/software/wt-2.99.1/doc/reference/html/classWt_1_1JSignal.html>,
you can emit a C++ signal from JavaScript code, using a JavaScript function
Wt.emit().
- Using WApplication::doJavaScript()<file:///home/cx/software/wt-2.99.1/doc/reference/html/classWt_1_1WApplication.html#2a92457b9212cef4057cb54e56183967>,
you can call JavaScript code directly as part of event handling.
Post by ZhiTao Chen
hi all,Is it possible with Wt to call a client javascript function with
parameters?
ex. There's a js function: function showMsg(msg) { alert(msg); }
How to call it just like alert('hello'); in C++ code?
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
witty-interest mailing list
https://lists.sourceforge.net/lists/listinfo/witty-interest
Wim Dumon
2009-08-06 06:25:29 UTC
Permalink
Post by ZhiTao Chen
hi all,
Is it possible with Wt to call a client javascript function with parameters?
ex. There's a js function: function showMsg(msg) { alert(msg); }
How to call it just like alert('hello'); in C++ code?
If you need to load a JS library, load it with WApplication::require(...).

void f(const std::string &msg)
{
std::stringstream ss;
ss << "showMsg(\"" << msg.c_str() << "\");";
wApp->doJavaScript(ss.str());
}

It is your responsibility to do output filtering (escaping of special
double quotes etc) in msg.

Regards,
Wim.
ZhiTao Chen
2009-08-07 10:28:36 UTC
Permalink
Thanks Zhimin & Wim, I tried Wim's method,It's OK!
BUT there's another question:
When I use Chinese message in the alert function, it didn't display
properly.
Maybe it's a problem about charset or encoding.
My C++ code is in GB2312,the document.charset is utf-8.
How to resolve this?
Koen Deforche
2009-08-07 10:59:49 UTC
Permalink
Hey ZhiTao,
Post by ZhiTao Chen
Thanks Zhimin & Wim,
I tried Wim's method,It's OK!
When I use Chinese message in the alert function, it didn't display
properly.
Maybe it's a problem about charset or encoding.
It most certainly is !
Post by ZhiTao Chen
My C++ code is in GB2312,the document.charset is utf-8.
How to resolve this?
doJavaScript() expects UTF8; it is a low-level method.

The easiest way to get it right is by not relying on the charset
encoding used by your compiler (or indeed your server locale). You can
avoid this by using WStrings stored in XML message resource bundles
(stored in UTF8 or UTF16 format for example), and the use
doJavaScript("alert(" + WString::tr("my-message").jsStringLiteral() +
")");

Regards,
koen
ZhiTao Chen
2009-08-13 02:11:29 UTC
Permalink
Thanks Koen!I didn't know there's a jsStringLiteral, it really works!
I found another solution a couple of days before:
Save the message in a text file in UTF-8,and read it at runtime.

Thank you ALL again.

Loading...