以下是这些案例的一些示例。请注意,额外的 f:secity.nonce() viewhelper 仅用于测试目的。
在这种情况下,viewhelper 可以正常工作:
{f:security.nonce()} <!-- Outputs hash 0NnkfpY8... -->
<script nonce="{f:security.nonce()}">
let map;
async function initMap() {
// Map
map = new Map(document.getElementById("map"), {});
}
initMap();
</script>
但是当向 zoom: 14,
对象添加类似
Map
的内容时,viewhelper 不会生成任何内容:
{f:security.nonce()} <!-- Outputs {f:security.nonce()} -->
<script nonce="{f:security.nonce()}">
let map;
async function initMap() {
// Map
map = new Map(document.getElementById("map"), {
zoom: 14,
});
}
initMap();
</script>
这是另一个例子:{f:security.nonce()} <!-- Outputs hash 0NnkfpY8... -->
<script nonce="{f:security.nonce()}">
let map;
async function initMap() {
// Set map constants
const centerPosition = { lat: xxx, lng: xxx },
mapStyle = "xxxxxx";
const markerPosition = centerPosition;
}
initMap();
</script>
但是像这样它不会:
{f:security.nonce()} <!-- Outputs {f:security.nonce()} -->
<script nonce="{f:security.nonce()}">
let map;
async function initMap() {
// Set map constants
const centerPosition = { lat: xxx, lng: xxx },
mapStyle = "xxxxxx",
markerPosition = centerPosition;
}
initMap();
</script>
第一个示例有效,第二个示例无效。唯一的区别是删除了 console.log()。
这有效:
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true" lang="">
<!-- Hello World {f:security.nonce()} -->
<script nonce="{f:security.nonce()}">
(function () {
const allSelectBoxes = document.querySelectorAll("select.bb-select.placeholder");
for (let item of allSelectBoxes) {
item.addEventListener("change", function (event) {
console.log("Hello World: {f:security.nonce()}");
if (this.selectedIndex === 0) {
this.classList.add("placeholder-shown");
} else {
this.classList.remove("placeholder-shown");
}
});
const event = new Event('change');
item.dispatchEvent(event);
}
})();
</script>
</html>
工作示例生成的 HTML:
<!-- Hello World _cEsvhNVZ6OgiKM8NI6L0u1sKYZKmOQxm4tf6O3f-eH00FmmvLue_g -->
<script nonce="_cEsvhNVZ6OgiKM8NI6L0u1sKYZKmOQxm4tf6O3f-eH00FmmvLue_g">
(function () {
const allSelectBoxes = document.querySelectorAll("select.bb-select.placeholder");
for (let item of allSelectBoxes) {
item.addEventListener("change", function (event) {
console.log("Hello World: _cEsvhNVZ6OgiKM8NI6L0u1sKYZKmOQxm4tf6O3f-eH00FmmvLue_g");
if (this.selectedIndex === 0) {
this.classList.add("placeholder-shown");
} else {
this.classList.remove("placeholder-shown");
}
});
const event = new Event('change');
item.dispatchEvent(event);
}
})();
</script>
这不起作用,因为我删除了第 8 行 console.log("Hello World:)
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true" lang="">
<!-- Hello World {f:security.nonce()} -->
<script nonce="{f:security.nonce()}">
(function () {
const allSelectBoxes = document.querySelectorAll("select.bb-select.placeholder");
for (let item of allSelectBoxes) {
item.addEventListener("change", function (event) {
if (this.selectedIndex === 0) {
this.classList.add("placeholder-shown");
} else {
this.classList.remove("placeholder-shown");
}
});
const event = new Event('change');
item.dispatchEvent(event);
}
})();
</script>
</html>
生成的不起作用示例的 HTML:
<!-- Hello World {f:security.nonce()} -->
<script nonce="{f:security.nonce()}">
(function () {
const allSelectBoxes = document.querySelectorAll("select.bb-select.placeholder");
for (let item of allSelectBoxes) {
item.addEventListener("change", function (event) {
if (this.selectedIndex === 0) {
this.classList.add("placeholder-shown");
} else {
this.classList.remove("placeholder-shown");
}
});
const event = new Event('change');
item.dispatchEvent(event);
}
})();
</script>