How to Fix the CKEditor File Browser JavaScript Error

If you’ve encountered the error Uncaught SyntaxError: Unexpected token } while working with CKEditor in your PHP project, you’re not alone. This JavaScript error usually pops up when you’re dealing with file browsing functionality in CKEditor, especially while trying to add an image through the file browser.

I’ll break down the code that causes the issue, explain why the error occurs, and suggest ways to fix it. Additionally, I will provide a more comprehensive solution with extra functionality for better error handling.

Understand the Error

Here’s the error you might be dealing with:

Uncaught SyntaxError: Unexpected token }

This error typically arises when there is a problem with how JavaScript is being generated or executed. It is often caused by improperly formatted code or invalid characters being injected into the JavaScript that CKEditor tries to execute.

The PHP Code

The provided PHP code is meant to generate a clickable image tag that, when clicked, inserts the image into CKEditor using CKEDITOR.tools.callFunction.

Here’s the original PHP code you might be working with:

$result ="
<a onclick='window.parent.CKEDITOR.tools.callFunction('$funcNum', '$url', '$message')'>
<img src='$url' style='width:200px; height:200px; float:right; padding:20px; background-color:#CCC; border:3px solid #CCC;' />
</a>
";
echo $result;

At a glance, this looks mostly correct, but it is prone to JavaScript issues, mainly because PHP variables are being inserted directly into JavaScript code. The problem lies in how PHP handles quotes and how the $funcNum and $url variables are being passed into the JavaScript. When PHP variables like $funcNum are inserted inside JavaScript code, it can mess up the syntax if proper escaping isn’t applied.

The Cause of the Syntax Error

The error is likely caused by how PHP is embedding variables inside JavaScript. The $funcNum, $url, and $message values are being injected directly into the JavaScript code, but their values may contain characters like quotes or other special characters that break the JavaScript syntax.

For example, the $url variable might contain characters that interfere with the JavaScript syntax, such as single quotes ('), leading to the error Unexpected token }.

Fix the Code

To resolve this issue, we need to properly escape the PHP variables to ensure they do not interfere with the JavaScript syntax. We can use addslashes() to escape single quotes and other special characters inside the variables before embedding them into the JavaScript string.

Here’s the updated PHP code with escaping:

$funcNum = addslashes($funcNum); // Escape the funcNum
$url = addslashes($url); // Escape the URL
$message = addslashes($message); // Escape the message

$result ="
<a onclick='window.parent.CKEDITOR.tools.callFunction(\"$funcNum\", \"$url\", \"$message\")'>
<img src=\"$url\" style=\"width:200px; height:200px; float:right; padding:20px; background-color:#CCC; border:3px solid #CCC;\" />
</a>
";
echo $result

In this updated code:

  • We use addslashes() to escape any special characters in $funcNum, $url, and $message.
  • We replace the single quotes with escaped double quotes (\") in the JavaScript code to ensure proper syntax.

This should resolve the Unexpected token } error.

Additional Functionality

Let’s enhance this functionality further by adding some checks and validations. Here’s how I improved the code to handle potential issues like missing or invalid parameters.

// Validate the necessary parameters
if (empty($funcNum) || empty($url)) {
echo "Error: Function number or URL is missing.";
exit;
}

// Validate the URL to ensure it's a valid image URL
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo "Error: Invalid URL.";
exit;
}

// Sanitize the message to prevent XSS attacks
$message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');

$funcNum = addslashes($funcNum);
$url = addslashes($url);

$result ="
<a onclick='window.parent.CKEDITOR.tools.callFunction(\"$funcNum\", \"$url\", \"$message\")'>
<img src=\"$url\" style=\"width:200px; height:200px; float:right; padding:20px; background-color:#CCC; border:3px solid #CCC;\" />
</a>
";
echo $result;

What We’ve Added:

  1. Parameter Validation: I’ve checked if $funcNum and $url are empty. If either of them is missing, I display an error message and exit.
  2. URL Validation: I’ve added a check to ensure the $url is a valid URL using filter_var(). This helps prevent broken links and ensures the image URL is valid.
  3. Message Sanitization: I’ve sanitized the $message variable using htmlspecialchars() to prevent XSS (cross-site scripting) attacks.

Conclusion

The Uncaught SyntaxError: Unexpected token } error occurs when JavaScript embedded in PHP code is improperly formatted. By escaping PHP variables correctly and validating inputs, we can avoid these errors and ensure that the CKEditor file browser works seamlessly.

Additionally, adding validation for parameters like $funcNum and $url can improve the overall functionality and security of the feature. Proper escaping and sanitization are key to preventing issues and keeping your code secure.

Related blog posts