最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Why does my Javascript return [object HTMLScriptElement] instead of expected text? - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Jan 18, 2011 at 9:47 KJWKJW 15.3k51 gold badges141 silver badges247 bronze badges 9
  • 2 What is someArray? How can you tell it's returning [object HTMLScriptElement]? – user447356 Commented Jan 18, 2011 at 9: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 Commented Jan 18, 2011 at 9:52
  • @Spudley: [object HTLMScriptElement] is the result of coercing a <script> element to a string anyway, so the result will not change. The problem isn't with the code here, it's where returnsomeText() is being called that's likely the issue. We need to see more code. – Andy E Commented Jan 18, 2011 at 9:57
  • Hi Kim, Can you return hi.ToString();? – WraithNath Commented Jan 18, 2011 at 9:57
  • @Kim, glad to here it, ill undelete my Answer! – WraithNath Commented Jan 18, 2011 at 10:12
 |  Show 4 more ments

3 Answers 3

Reset to default 6

Try return hi.toString();

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

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

发布评论

评论列表(0)

  1. 暂无评论