S
S
Sergey Sobol2021-07-07 13:50:23
JavaScript
Sergey Sobol, 2021-07-07 13:50:23

How can I make the script work only on one page?

Tell me how to make the script work only on one specific page?

It is necessary so that on all pages there is no error about not finding a class or ID.

Script:

const headerWindow = document.getElementById('header_window'),
    openHeaderWindow = document.getElementById('control_avatar'),
    closeHeaderWindow = document.getElementsByClassName('close_window')[0];

openHeaderWindow.onclick = function () {
  headerWindow.style.display = 'block';
}
closeHeaderWindow.onclick = function () {
  headerWindow.style.display = 'none';
}
window.onclick = function(e) {
  if (e.target == headerWindow) {
    headerWindow.style.display = 'none';
  }
}


Screenshot of the error on the other pages:
60e586eb46728571559271.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FeST1Val, 2021-07-07
@FeST1Val

Check if there is such a selector on the page and then execute the code

A
Alex, 2021-07-07
@Kozack

Make sure that this script is present only on the desired page, and not on the rest.

K
kryamk, 2021-07-07
@kryamk

if( !headerWindow ) return

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question