-1
我使用javascript創建了一個彈出窗口(模式),它警告人們我們在我們的網站上使用cookie。 點擊時有一個接受按鈕應創建一個持續60天的cookie並阻止顯示模式。但是當我點擊按鈕,我得到一個錯誤,說我不能修改標題,因爲它已經發送。Cookie和模式錯誤
模態JavaScript是從w3schools和作品,但我的瀏覽器得到一個錯誤「無法設置屬性'空'onclick'爲空」。
繼承人的代碼:
<?php
$cookie_name = "V3-cookies";
if(!isset($_COOKIE[$cookie_name])) {
include $_SERVER["DOCUMENT_ROOT"] . "/etc/cookie.php";
} else {}
?>
這是/etc/cookie.php:
<form id="modal" method="post" action="">
<div class="modalconent tabs">
<fieldset>
<span class="close">×</span>
<h2 class="fs-title">Cookies</h2>
<h3 class="fs-subtitle">We use cookies to improve your experience</h3>
<iframe style="width:100%; height:80px;" src="/etc/txt/cookies.php" ></iframe><br />
<input type="submit" name="cookie" value="Accept" class="btn fr" style="width:20%;" />
</fieldset>
</div>
</form>
<?php
if(isset($_POST['cookie'])){
$cookie_name = "V3-cookies";
$cookie_value = "V3-cookies";
setcookie($cookie_name, $cookie_value, time() + (86400 * 60)); // 86400 = 1 day
}else{}
?>
<script>
window.onload = function() {
document.getElementById('button').onclick = function() {
document.getElementById('modal').style.display = "none"
};
};
// Get the modal
var modal = document.getElementById('modal');
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
的JavaScript錯誤的位置屬性 '點擊數' 爲空這裏 (第二線)
window.onload = function() {
document.getElementById('button').onclick = function() {
document.getElementById('modal').style.display = "none"
};
};
頭php的錯誤我不知道出了什麼問題。
所以總共有2個問題。 1.我怎樣才能修復PHP設置按鈕點擊cookie? 2.如何刪除onlick爲空的瀏覽器錯誤? (2.是不是真的那麼重要,只是我討厭有錯誤)
您需要重新組織您的PHP代碼,以便在頁面之前發送HTTP標頭,而不是在HTML中間發送。這與JavaScript無關。 –
嗯,你甚至有一個可點擊的元素與id #button? – ravb79
@ ravb79編號的JavaScript是從W3被採取的,我說它的工作。但是我得到了一個錯誤,真正的問題是PHP,在接受按鈕被按下時,我無法設置cookie。 –