Hi all,

I'm modifying the mfcembed application to execute javascript from the C++ code.

After searching around the threads in this alias, I got below piece of code, which compilers, but
won't work. I tried to use JS_EvaluateScript to execute script "alert(\'This is written from code!')"
and "document.write(\"This is written from code!\")" but both failed.


Could anybody give me any hints to investigate ?

Thanks,

-George Z.

--- Snip ---
nsresult GetContextFromDocument(nsIDocument *doc, JSContext **cx)
{
nsCOMPtr<nsIScriptGlobalObject> sgo;
doc->GetScriptGlobalObject(getter_AddRefs(sgo));
if (!sgo) {
    // No script global, no context.
    return NS_OK;
}

nsCOMPtr<nsIScriptContext> scx;
sgo->GetContext(getter_AddRefs(scx));
if (!scx) {
  // No context left in the old scope...
  return NS_OK;
}

*cx = (JSContext *) scx->GetNativeContext();
return NS_OK;
}

void CBrowserView::OnNavBack()
{
//    if (mWebNav)
//        mWebNav->GoBack();

mWebNav->LoadURI(NS_ConvertASCIItoUCS2("file:///C:/test.html").get(),
                 nsIWebNavigation::LOAD_FLAGS_NONE,
                 nsnull,
                 nsnull,
                 nsnull);

nsCOMPtr<nsIDOMDocument> domDoc;
mWebNav->GetDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));

// gets the JSContext
JSContext *cx;
GetContextFromDocument(doc, &cx);

char *script = "alert(\'This is written from code!')";
//char *script = "document.write(\"This is written from code!\")";

jsval rval;
JSString *str;
JSBool ok = JS_EvaluateScript(cx, JS_GetGlobalObject(cx), script,
                              strlen(script), NULL, NULL, &rval);
}
--- Snip ---

// I used below sample test.html for the test.
<html>
<head>
<script>var counter = 1;
</script>
</head>
<body>
<div id='theDiv'>test text</div>
</body>
</html>
_______________________________________________
mozilla-embedding mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-embedding

Reply via email to