Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:45:48+00:00 2026-06-14T04:45:48+00:00

I am trying to output Google Translate code from PHP (wordpress plugin) to the

  • 0

I am trying to output Google Translate code from PHP (wordpress plugin) to the page and it is giving me Illegal Token errors even though when I paste the exact same line of code directly into the console and run it, it works.

Here is my PHP:

    function AddTranslateSidebar(){
  echo "<script>
    jQuery(document).ready(function(){
      gt = escape('<div id=\"google_translate_element\"></div><script type=\"text/javascript\">function googleTranslateElementInit() {new google.translate.TranslateElement({pageLanguage: \"en\", layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, \"google_translate_element\");}</script><script type=\"text/javascript\" src=\"http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit\"></script>');
      jQuery('#sidebar').prepend(unescape(gt));
    });
  </script>";
}

That code outputs the following into the page as HTML/JavaScript:

<script>
    jQuery(document).ready(function(){
      gt = escape('<div id="google_translate_element"></div><script type="text/javascript">function googleTranslateElementInit() {new google.translate.TranslateElement({pageLanguage: "en", layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, "google_translate_element");}</script><script type="text/javascript" src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>');
      jQuery('#sidebar').prepend(unescape(gt));
    });
  </script>

Which causes the error

Uncaught SyntaxError: Unexpected token ILLEGAL 

On the gt = escape( … ); line.

However, if I copy that code directly from the HTML source using the inspector and then paste it into the console and run it, it works and the translate dropdown shows up.

Why is this happening?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:45:50+00:00Added an answer on June 14, 2026 at 4:45 am

    The sequence of characters </script> always means to end the current script block. My suspicion is that this is causing the problem.

    You can’t include </script> verbatim in a JavaScript string. Instead you need to break it up and re-concatenate the parts.

    E.g. this:

    "</script>"
    

    Should become this:

    "<" + "/script>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to output the comments of a facebook page in PHP. For
I'm trying to figure out how to redirect output from some FORTRAN code for
I'm trying to use the wordalignment in the BerkeleyAligner.jar file from http://code.google.com/p/berkeleyaligner/ in my
I m trying to output introtext of article selected by their alias <?php $db
I'm trying to output unicode string into RTF format. (using c# and winforms) From
I'm trying to use the web-based google translate to translate my english files to
I am trying to output a simple link. This works great: $url = 'http://www.google.com';
I'm trying to get the following code to output an IMG tag with the
I'm trying to get the hits of a google search from a string of
I am trying to get the realtime stockquotes from google for some time now.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.