KJW
KJW

Reputation: 15251

Why does my Javascript return [object HTMLScriptElement] instead of expected text?

I am having similar issues from

unable to run an external javascript using a bookmarklet.

But I am executing my JavaScript inside a Java application via injecting script headers into the current DOM loaded via Java application.

This problem seems to occur randomly. Some cases it returns [object HTMLScriptElement] and other times returns the text...

When I alert() the object, it returns text!

I have tried return String(hi); but still no effect.

function returnsomeText(){
    var hi = someArray.join(':');
    alert(hi); //returns text:text:text:text as expected.
    return hi; //returns [object HTMLScriptElement]
}

I am very confused to as what is causing this problem! If JavaScript returns [object HTMLScriptElement] then my Java application cannot process the text.

This question is in more detail here:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException when trying to execute Javascript

Upvotes: 3

Views: 10388

Answers (3)

ReNuX
ReNuX

Reputation: 16

try hi.outerHTML and there is also an innerHTML, just saying so it may come handy someday

Upvotes: 0

Dave Brown
Dave Brown

Reputation: 939

TRY adding .text somewhere like:

function returnsomeText(){
    var hi = someArray.join(':');
    alert(hi); //returns text:text:text:text as expected.
    return hi.text;
}

HERE is a demo:

document.write(document.body.children[3]); //writes [object HTMLScriptElement]
document.write(document.body.children[3].text); //writes text data

Upvotes: 0

WraithNath
WraithNath

Reputation: 18013

Try return hi.toString();

Upvotes: 7

Related Questions