I am new to javascript. I was trying to make a message box popup onload. As i am beginner i copied the code from internet just to see how it works but for some reason the pop is not showing up. I know the code is not worng because it is wrking in the video. the calling method is a bit different as i am using django.
console error
auto.js:6 Uncaught TypeError: Cannot read property 'style' of null
html code
{% block content %}
<script type="text/javascript" src="{% static 'auto.js' %}"></script>
<div class="popup">
<div class="contentBox">
<div class="close"></div>
<h3 style="color: oldlace;">TESTING POPUP</h3>
</div>
</div>
{% endblock %}
javasript code
const popup = document.querySelector('.popup');
window.onload = function(){
setTimeout(function(){
popup.style.display = "block"
// add some time delay o show
}, 2000)
}
asked Jul 29, 2021 at 20:21
Ritankar Bhattacharjee
7762 gold badges11 silver badges36 bronze badges
2 Answers 2
You need to load js file after html:
{% block content %}
<div class="popup">
<div class="contentBox">
<div class="close"></div>
<h3 style="color: oldlace;">TESTING POPUP</h3>
</div>
</div>
<script type="text/javascript" src="{% static 'auto.js' %}"></script>
{% endblock %}
Sign up to request clarification or add additional context in comments.
1 Comment
Ritankar Bhattacharjee
thank you so much now it is working. thank you for the help
the syntax s document.querySelector(Selector) I do not think .popup is a selector from your listing.
1 Comment
Ritankar Bhattacharjee
thank you so much, now it is working fine kamran's advice worked for me
default
popupvariable is null .You can always try portions of Javascript directly on the browser console , likedocument.querySelector('.popup'). So maybe you need to include that declaration inside setTimeout function .