Добрый день, штабелёры!
Может ли кто-нибудь помочь мне понять, почему простая функция onchange javascript работает в одной папке, но не работает в другой папке на том же веб-сервере (cPanel v120.0.16).
Ссылки ниже перенаправят вас в нерабочую и рабочую папку.
Обе папки имеют одинаковые разрешения и тип. Обе папки содержат файл test.php с тем же кодом, что и ниже.
<!DOCTYPE html>
<html>
<body>
<p>Select a new car from the list.</p>
<select id = "mySelect" onchange = "myFunction()">
<option value = "Audi">Audi</option>
<option value = "BMW">BMW</option>
<option value = "Mercedes">Mercedes</option>
<option value = "Volvo">Volvo</option>
</select>
<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>
<p id = "demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>
Любые указатели или решения будут иметь огромную помощь. Теплые пожелания
В консоли рабочего сайта (https://rohigroupofschools.com/presite/test.php ) ошибок не вижу. В консоли нерабочего сайта ( https://rohigroupofschools.com/app/test.php) я вижу десятки сообщений об ошибках типа
Content-Security-Policy: The page’s settings blocked an event handler (script-src-attr) from being executed because it violates the following directive: “default-src 'self'” Source: myFunction() test.php
Content-Security-Policy: The page’s settings blocked an inline script (script-src-elem) from being executed because it violates the following directive: “default-src 'self'” test.php:18:9
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459Wlhyw.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459Wdhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WZhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459W1hyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WRhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459Wlhyw.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459Wdhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WZhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459W1hyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WRhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459Wlhyw.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459Wdhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WZhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459W1hyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WRhyzbi.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfBBc4.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfChc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfCxc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfBxc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfCBc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfABc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmWUlfCRc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fBBc4.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fChc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fCxc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fBxc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fCBc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fABc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOlCnqEu92Fr1MmEU9fCRc4EsA.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu4mxK.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu7GxKOzY.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu7WxKOzY.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu4WxKOzY.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu7mxKOzY.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu5mxKOzY.woff2 because it violates the following directive: “default-src 'self'” test.php
Content-Security-Policy: The page’s settings blocked the loading of a resource (font-src) at https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu72xKOzY.woff2 because it violates the following directive: “default-src 'self'” test.php
Настроенные политики безопасности контента (CSP) блокируют выполнение некоторых встроенных скриптов и обработчиков событий, а также загрузку некоторых внешних ресурсов.
Текущий CSP можно найти в заголовке ответа. Нерабочий сайт имеет заголовок
content-security-policy: default-src 'self'; form-action 'self'; object-src 'none'; frame-ancestors 'none'; upgrade-insecure-requests; block-all-mixed-content
На рабочем сайте такого заголовка нет.
Обработчик событий
onchange = "myFunction()"
в
<select id = "mySelect" onchange = "myFunction()">
<option value = "Audi">Audi</option>
<option value = "BMW">BMW</option>
<option value = "Mercedes">Mercedes</option>
<option value = "Volvo">Volvo</option>
</select>
и элемент сценария
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
заблокированы CSP.
Вам необходимо изменить конфигурацию CSP или изменить свой код таким образом, чтобы он учитывал CSP.
script-src-attr self
не разрешает встроенные обработчики событий и script-src-elem self
не разрешает встроенные скрипты. Один из способов решения проблемы — переместить весь код JavaScript в отдельные файлы одного происхождения и загрузить файлы сценариев.
Также рекомендуется избегать обработчиков событий on...
, см. Почему использование onClick() в HTML — плохая практика? и встроенный JavaScript см. Чем встроенный JavaScript плох?. CSP заставляет вас избегать этого.
Большой! Теперь это работает. Спасибо за подсказку по проверке.
попробуйте переименовать оба файла в
html
вместоphp
или опубликуйте воспроизводимый пример, пожалуйста