In the following code
<style>
/* Set height to 100% for body and html to enable the background image to cover the whole page: */
body, html {
height: 100%
}
body {
background-image: url('/assets/root/001.png');
// background-size: contain;
// background-position: center;
background-size: 786px 594px;
// background-size: 1920px 933px;
background-repeat: no-repeat;
}
</style>
<div>
<h1>Some message</h1>
<button type="button" onclick="myFunction()">Get background image</button>
<script>
function myFunction() {
debugger;
alert(document.body.style.backgroundImage);
}
</script>
<script language="JavaScript" type="text/javascript">
var width = $(window).width();
var height = $(window).height();
/* debugger; */
alert(document.body.style.backgroundImage);
alert('Window is ' + width + ':' + height)
</script>
</div>
I expect
alert(document.body.style.backgroundImage);
to display something like '/assets/root/001.png' but the alert shows nothing in both Firefox(61.0.1 (64-bit)) as well a Chrome(Version 68.0.3440.84 (Official Build) (64-bit)) both when I refresh the page or click on the "Get background image" button.
In the Firefox debugger I see the value of document.body.style.backgroundImage as "" when I click on the "Get background image" button and thus hit the debugger statement.
Obviously, I'm using jQuery.
N.B. The background image does display on the webpage. Also, I don't see any other errors in the debugging console. Also the
alert('Window is ' + width + ':' + height)
seems to output the correct values.
I don't know if this is relevant but I'm in a Rails environment.
What am I doing wrong?