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

javascript - problem with using getElementById().getValue in FBJSFacebook ! - Stack Overflow

programmeradmin2浏览0评论

I am using this code to generate hidden HTML code in Facebook :

echo "<div id=\"y257y\" style=\"display:none;\">".$fdesc."</div>";

And, I am trying to get this element back in JS using the following code

newVal=document.getElementById('y257y').getValue();

But, I am getting an error: Undefined

Can anyone kindly help me out ?

Thanks.

- ahsan

I am using this code to generate hidden HTML code in Facebook :

echo "<div id=\"y257y\" style=\"display:none;\">".$fdesc."</div>";

And, I am trying to get this element back in JS using the following code

newVal=document.getElementById('y257y').getValue();

But, I am getting an error: Undefined

Can anyone kindly help me out ?

Thanks.

- ahsan

Share Improve this question edited Mar 13, 2011 at 2:49 Ahsan asked Mar 13, 2011 at 2:18 AhsanAhsan 2,98212 gold badges54 silver badges97 bronze badges 6
  • Just a quick remendation: echo "<div id='y257y' style='display:none;'>".$fdesc."</div>"; – JCOC611 Commented Mar 13, 2011 at 2:24
  • 1 Even better: <div id="y257y" style="display:none;"><?php echo $fdesc; ?></div> | What makes you think the exists the function getValue() ? – Felix Kling Commented Mar 13, 2011 at 2:29
  • @Felix Kling: I am working wth FBJS and theres a function getValue() as mentioend here : developers.facebook./docs/fbjs – Ahsan Commented Mar 13, 2011 at 2:48
  • @Ahsan: I see. But this table shows a translation of JavaScript attributes to FBJS methods. The attribute value translates to getValue() and only form elements have this attribute. It was never possible to get the content of an arbitrary element with value. From what I read on this page, it seems you just can't access the the HTML. I hope you also read the note at the top. It seems the don't remend to use FBML anymore. – Felix Kling Commented Mar 13, 2011 at 2:53
  • Hidden HTML in Facebook, eh? Sounds ominous. – Lightness Races in Orbit Commented Mar 13, 2011 at 3:36
 |  Show 1 more ment

2 Answers 2

Reset to default 4

Instead of:

newVal = document.getElementById('y257y').getValue();

try using:

newVal = document.getElementById('y257y').innerHTML;

Are you using any JavaScript library, like jQuery or Prototype? If you're using jQuery:

newVal = $('#y257y').html();

Other suggestions:

Use hidden form element:

echo "<input type=hidden id=y257y value=\"$fdesc\">";

and in JavaScript:

newVal = document.getElementById('y257y').value;

Or just output a <script> tag:

echo "<script>newVal = \"$fdesc\";</script>";

and there's no need to find the value in the DOM – it's already in JavaScript.

Try using a textarea? Since at least that may have a getValue method if you are really lucky:

$fdesc = str_replace('<', '&lt;', $fdesc);
$fdesc = str_replace('>', '&gt;', $fdesc);

echo "<textarea id=\"y257y\" style=\"display:none;\">".$fdesc."</textarea>";
newVal=document.getElementById('y257y').getValue();
newVal = newVal.replace("&gt;", ">")
newVal = newVal.replace("&lt;", "<")

But something tells me that is not going to work for you, facebook may not like it. Seems easier just to do this:

$fdesc = <<<EOD
 <div>Here is your html</div>
 <p>And some more </p>
EOD;

$fdesc = str_replace("'", "\'", $fdesc);   //Escape single quotes
$fdesc = str_replace("\n", "";', $fdesc);   //Get rid of line breaks

 echo "var fdesc = '$fdesk';";
发布评论

评论列表(0)

  1. 暂无评论