简体   繁体   中英

Trying to make two pieces of premade javascript code function together

I don't know anything about javascript but I can't get these two scripts to work together, if anyone could fix my mistakes I'd be very grateful. The scripts are premade from http://www.mf2fm.com/rv/ and function just fine on their own. I've tried to solve the issue myself but I'm unfortunately entirely clueless. I got this error when I tried to test running what I pasted below on a test site; "JavaScript error: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. on line 48" Line 48 being: document.body.appendChild(stars[j]); if that's any help. The problem seems to be localized to these lines, though they run fine without the second script.

addLoadEvent(clicksplode);

function clicksplode() { if (document.getElementById) {
  var i, j;
  window.onscroll=set_scroll;
  window.onresize=set_width;
  document.onclick=eksplode;
  set_width();
  set_scroll();
  for (i=0; i<bangs; i++) for (j=sparks*i; j<sparks+sparks*i; j++) {
    stars[j]=createDiv('*', 13);
    document.body.appendChild(stars[j]);
  }
}}

Well, for one thing, you're copying a script that dates back to 2002 at the very latest. These belong in a museum. I had a whole nostalgic flashback to the 90s loading up that site from your source code.

The error you're getting is that value of stars[j] is not a DOM Node, so appendChild() doesn't know what to do with it.

Since it seems like you're already familiar with the dev console, I'd suggest putting a breakpoint in at that line 48 where you see your error and seeing what is actually being passed to appendChild(). Then you can walk up the stack trace to see where that bad value is coming from. You can also, in chrome dev tools, turn on "pause on exceptions" to automatically stop your code in the debugger when an error is thrown. It's the icon of a pause button in a circle in the upper right of the source tab in the chrome debugger.

Best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM