Search code examples
javascripthtmlbookmarklet

Javascript code to add HTML code to body tag


I have a webpage which I am experimenting with, where the user has to have a specific bookmarklet from another one of my sites to continue the game. This bookmarklet adds code to the tag, and enables the user to continue.

The problem is that I am having trouble making the bookmarklet, since I am fairly new at using javascript.

Can anyone help me with this?


Solution

  • To add javascript to the body, try this:

    var code = '<h1>TEST</h1>';
    document.body.innerHTML = code;
    

    You will have to play with it to get what you want to do, but that's the general idea. If you have a particular div you want to use, try doing the same thing but with document.getElementById