0

I am trying to add an HTML marquee element before the body element. I want to achieve this with javascript. I have searched all over for a way to do this but no success. Please help me.

One way I thought of doing it was

document.insertBefore('<marquee>', '<body>')

This gives me the error of

VM888:1 Uncaught TypeError: Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'.

Please help. Thank You!

Hassan Sani
  • 124
  • 2
  • 9
  • 2
    You're inserting a String that says ``, not an actual HTML element. You'll also want to insert the element into the `body`: https://jsfiddle.net/9udmks3g/ –  Nov 27 '17 at 23:30

1 Answers1

3

There are a number of problems with what you're trying to do:

  1. The <marquee> element is obsolete; don't use it. Ever.
  2. You shouldn't even be attempting to insert before <body>.
    The only valid children of <html> are <head> and <body>, specifically in that order.
  3. Third, you need to insert a DOM node, not an HTML element (a type of node):

// Create the node to insert
var newNode = document.createElement("span");
newNode.innerHTML = 'new';

// Set up the other nodes
var parent = document.getElementById("childElement").parentNode;
var child = document.getElementById("childElement");

// Insert it
parent.insertBefore(newNode, child);
<div id="parentElement">
   <span id="childElement"> existing</span>
</div>

Hope this helps :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Thank you! Also, why is marquee 'Obsolete'? – That One Person XboxPcGamer24 Nov 27 '17 at 23:48
  • No problems! I'd conjecture that it's because of the effect it might have on usability. There's also the fact that mobile browsers don't have the memory to spare to support it correctly. There's more information (including possible alternatives) in [**this SO question**](https://stackoverflow.com/questions/31951282/why-is-marquee-deprecated-and-what-is-the-best-alternative), which may be of use :) – Obsidian Age Nov 27 '17 at 23:50