Why am I getting the error "unexpected token ILLEGAL" in this script? The script is supposed to retrieve all cookies and display their names and values in a table. The error occurs on the line that has the opening table tag.
document.getElementById("showCookies").onclick = function(){
var columnRight = document.getElementById('columnRight');
var cookies = document.cookie;
var cookiesContent = '<h1>Existing Cookies</h1>';
console.log(cookies);
cookies = cookies.split(";");
if(cookies.length > 0){
cookiesContent += '<table width="30%" cellspacing="1" cellpadding="10" border="0">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
';
for(var i = 0; i < cookies.length; i++){
cookieAtts = cookies[i].split('=');
cookiesContent += "<tr><td>" + cookieAtts[0] + "</td><td>" + cookieAtts[1] + "</td></tr>";
}
cookiesContent += "</tbody></table>";
}
columnRight.innerHTML = cookiesContent;
return false;
}
Use string concatenation to define long strings:
cookiesContent += '<table width="30%" cellspacing="1" cellpadding="10" border="0">' +
'<thead>'+
'<tr>' +
'<th>Name</th>'+
'<th>Value</th>'+
'</tr>'+
'</thead>'+
'<tbody>';
It's because you can't have multiline strings in JavaScript without backslashes:
cookiesContent += '<table width="30%" cellspacing="1" cellpadding="10" border="0"> \
<thead> \
<tr> \
<th>Name</th> \
<th>Value</th> \
</tr> \
</thead> \
<tbody> \
';
您在字符串中使用<thead...
换行了,这是不允许的
Concatenate this:
cookiesContent += '<table width="30%" cellspacing="1" cellpadding="10" border="0">' +
'<thead> ' +
'<tr> ' +
'<th>Name</th>' +
'<th>Value</th>' +
'</tr>' +
'</thead>' +
'<tbody>';
Or in the same line:
cookiesContent += '<table width="30%" cellspacing="1" cellpadding="10" border="0"><thead><tr><th>Name</th><th>Value</th></tr></thead><tbody>';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.