3

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

Community
  • 1
  • 1
KJW
  • 15,035
  • 47
  • 137
  • 243
  • 2
    What is `someArray`? How can you tell it's returning `[object HTMLScriptElement]`? – Shadow The GPT Wizard Jan 18 '11 at 09:49
  • if all else fails I guess you could try `''+hi` to force it to be a string. that doesn't actually answer the question though. – Spudley Jan 18 '11 at 09:52
  • @Spudley: `[object HTLMScriptElement]` is the result of coercing a ` – Andy E Jan 18 '11 at 09:57
  • Hi Kim, Can you return hi.ToString();? – WraithNath Jan 18 '11 at 09:57
  • @Kim, glad to here it, ill undelete my Answer! – WraithNath Jan 18 '11 at 10:12
  • @Kim you said "occur randomly" - so how can you be so sure it's working? Don't you need to wait some time and verify it's not happening again? – Shadow The GPT Wizard Jan 18 '11 at 11:41
  • there's something fishy going on if replacing `String(hi)` with `hi.toString()` solves the problem (overwriting the global `String()` would do it...); assuming `someArray` is actually an array, adding `toString()` is superfluous: `join()` already returns a string on any conforming ECMAScript implementation; also, the string representation of a script node is apparently `'[object HTMLScriptElement]'` in the given implementation, so `hi.toString()` shouldn't have fixed anything even if `hi` were a node - you'd have to use `hi.firstChild.nodeValue` – Christoph Jan 18 '11 at 12:01
  • I don't think Kim is showing us the code which causes the problem... – Christoph Jan 18 '11 at 12:03
  • @Christoph: details are here....this problem still not fixed. http://stackoverflow.com/questions/4732618/exception-in-thread-awt-eventqueue-0-java-lang-nullpointerexception-when-trying – KJW Jan 19 '11 at 09:01

3 Answers3

7

Try return hi.toString();

mplungjan
  • 169,008
  • 28
  • 173
  • 236
WraithNath
  • 17,658
  • 10
  • 55
  • 82
  • @Box9: he did and Kim Jong Woo said "Wow that worked" so I removed the Can you and the ? – mplungjan Jan 18 '11 at 10:15
  • Hi Kim, mplungjan was just helping format my code so it was more of an answer rather than a question. Not sure where the -1 vote came from though!! this answer was posted by me :) – WraithNath Jan 18 '11 at 11:10
  • Kim: it says EDITED by me, but Wraith will get the rep. I just changed the "Can you" to "Try" and removed the question mark. I also changed ToString to toString and voted this back up – mplungjan Jan 18 '11 at 11:11
  • @WraithNath and @mplungjan, sorry I take my comment back. – David Tang Jan 18 '11 at 11:20
  • @Box you can notify people only one at a time, meaning only the first `@` is taken into account - mplungjan will never see your last comment. The author of the commented Answer or Question will always get notification by the way for any comment made on his/her Answer or Question. Last but not least, you can use the first 3 letters e.g. @mpl will work just fine. :) – Shadow The GPT Wizard Jan 18 '11 at 11:40
  • @Box yeah.. learned of all this only recently myself after posting in `Meta` website, so decided to spread that knowledge around to the best of my ability. :) – Shadow The GPT Wizard Jan 18 '11 at 11:50
  • oops sry I was quite drunk lastnight. – KJW Jan 19 '11 at 00:51
  • hi please see this question, it is much more detailed, with codes provided. http://stackoverflow.com/questions/4732618/exception-in-thread-awt-eventqueue-0-java-lang-nullpointerexception-when-trying – KJW Jan 19 '11 at 09:02
  • @KimJongWoo feel free to delete your earlier comment about rep theft. Thanks – mplungjan Jan 20 '11 at 13:54
  • @Sha how does SO know the difference between two people with identical first 3 letters? Should happen one day.. – mplungjan Jan 20 '11 at 13:56
  • @mpl it doesn't know, the message is sent to the most recently active member. For example if Kim would have been called "mpl Jong Woo" this very message would still reach you since you commented after him, and to reach him I could use "@mplJongWoo" - note that blank spaces must be removed as well. – Shadow The GPT Wizard Jan 20 '11 at 14:03
  • sorry to inform you guys bu this is not the correct answer. Unaccepting this answer. – KJW Jan 21 '11 at 00:21
0

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
Dave Brown
  • 923
  • 9
  • 6
0

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

ReNuX
  • 16
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '22 at 05:40