开发者

HTML chunk appears in DOM only when Firebug is running

I've been banging my head against this for the past few hours.

I'm building an HTML chunk with Javascript and appending it to the page's DOM. Problem is this works only if Firebug is running and disabling Firebug doesn't help. When Firebug isn't running the code gets generated but not appended to the DOM (it's not that it's invisible or something).

Safari and Chrome are immune to the problem.

What should I look into?

Following is the incriminated code: if it looks weird it's because it is, and i'm just now refactoring it from the its original german black magic style without comments to self-explanatory jquery

function create_button() {

  var textblock = $('div#textblock2');
  var token;

  token = $(textblock).text();
  token = token.split('=')[1]; 
  //delete the text that we parsed to build the btn
  textblock.text(''); 

  var form = write_form();
  var btn = write_submit_btn(token);

  console.log('form: ' + form);
  console.log('btn: ' + btn);

  textblock.append(form);
  textblock.append(btn);

  console.log('textblock2 contents:' + textblock.html());

}

function write_form() {

  return "<form name='Formular'></form>";

}

function write_submit_btn(token) {
  var btn;

  if ( token.match(/weiter/) != null )

    btn = "<input type='button' name='naechsteFrage' value='weiter' onClick='load_next_question();' />";

  else

    btn = "<input 开发者_如何学编程type='button' name='naechsteFrage' value='zur" + String.fromCharCode(252) + "ck' onClick='load_prev_question()' />";

  return btn;
}


create_button();


console.log('textblock2 contents:' + textblock.html()); needs to be removed ... as does

  console.log('form: ' + form);
  console.log('btn: ' + btn);

When Firebug is not up, Firefox does not have a console and the function terminates.

All Webkit-based browsers have a built-in console, and are thus immune to this problem. (IE and Opera, on the other hand, would also have this problem. [And Links too, but I don't think you are worried about that. ;-) ])


Any calls to "console" methods will fail if firebug is closed. That's the issue.


Although you should always remove console functions in a live environment, the following code will ensure that any console function being run does not give a javascript error when there is no console:

if(!window.console||!console.firebug){var names=["log","debug","info","warn","error","assert","dir","dirxml","group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"];window.console={};for(var i=0;i<names.length;++i)window.console[names[i]]=function(){}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜