The script itself is fine but the problem is when it is executed. It tries to get an element before it even exists in the DOM.
A simple solution would be to put the script at the end of the body, when the required element is ready. Another approach is to let the document let you know when it's ready and you'll run your scripts after.
Refer to this answer for more information: Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it
Also as a general rule for javascript development, the developer tools available in all major browsers, are your friend.
Looking at the Console in chrome dev tools you would see this:
Uncaught TypeError: Cannot read property 'style' of null
at (index):5
Which hints that document.getElementById("p1")
returns null, in other words, an element with id p1 was not found.
Also if what you intend is to actually set the site's full background then you actually need to set it to the site's body. Just as if you were doing it using CSS.
document.body.style.backgroundImage = 'url("' + imageURL + '")';