Error: SyntaxError - Received an unexpected % symbol when parsing

Currently, I am facing an issue with my page that has multiple embedded YouTube players. I need to listen for events on these players but encounter a strange error while using a solution mentioned in the answer found at Using Youtube's javascript API with jQuery. The error I receive is as follows (observed in Chrome 18.0.1025.137 beta-m):

Uncaught SyntaxError: Unexpected token %

This error occurs when the onStateChange event is triggered. My code snippet looks like this:

var onYouTubePlayerReady = function (id) {
var evt = '(function(){})';

alert(eval(evt)); //just to ensure syntactical correctness of the snippet

var ytplayer = document.getElementById(id);
ytplayer.addEventListener("onStateChange", evt);
};

(for more details, refer to the linked question)

The error occurs specifically when the onStateChange event is fired. If I set evt to an empty string or a function name, the error does not occur (but unfortunately, no state information is received either).

While it seems like the error message is misleading, does anyone know if what I am attempting to achieve is actually possible?

Answer №1

Turns out, it was pretty dumb after all. When I ran the code in Firefox, it actually gave me a proper error message:

syntax error


try { __flash__toXML(%28function%28%29%7B%7D%29(5)) ; } catch (e) { "<undefined/...
---------------------^

So, it seems that the callback needs to be serializable to XML in order to work properly. Quite frustrating.

To solve this issue, I came up with something like this:

var ytCallbackID = 1;

var makeYTCallback = function (func) {
    var ret = "ytCallback" + (ytCallbackID++);
    window[ret] = func;

    return ret;
}

var onYouTubePlayerReady = function (id) {
    var evt = makeYTCallback(function(state) {
        onYouTubePlayerStateChange(id, state);
    });

    var ytplayer = document.getElementById(id);
    ytplayer.addEventListener("onStateChange", evt);
};

I haven't had a chance to fully test it yet (since I did this at the end of the day yesterday), but I'm hopeful it will work.

I finally got around to testing it, and it works perfectly! It might not be the prettiest solution, but it gets the job done, and that's what matters :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute the script on the server, rather than in the browser

I have a script named "script.php" that automates the renewal process for user orders. To streamline this process, I created a button that, when clicked, triggers the execution of the script. Here's the button code: <?php echo Html::anchor('. ...

What is the best way to retrieve the post JSON data in the event of a 404 error?

When my service call returns a 404 error, I want to display the server's message indicating the status. The response includes a status code and message in JSON format for success or failure. This is an example of my current service call: this._trans ...

After employing a combination of forEach and filter queries in JavaScript to sift through dropdown options, no results are being generated

Hey there fellow developers, I'm currently in the process of developing an app and have reached the stage where I am filtering elements using dropdowns. I am querying a JSON object and comparing it with the selected element in the dropdown at that mom ...

The successful function for file uploads is experiencing technical difficulties in the online edition

Within my PHP classes, I have the following code snippets: ... success: function(response){ if(response==='Successs'){ $('.'+input.id + ' .info_danger_text').html(''); $('.'+input.id + ' ...

Maintaining TextBox State in ASP.Net Through Postbacks

Can anyone help me figure out how to maintain the control state that has been modified in Javascript? I am working with two TextBoxes, one DropDownList, and a button (all Runat=Server) in C# ASP.net 2010 Express. The first textbox accepts any user in ...

jQuery: add to either element

What is the most efficient way to use .appendTo for either one element or another based on their existence in the DOM? For instance, I would like to achieve the following: Preferred Method: .appendTo($('#div1') || $('#div2')); Less ...

Utilize JavaScript to send an email containing a link and content

In the html form, there are input fields for adding a new user to the database. Once a user is added, an email is sent to them using the sendmail() function in the adduser.js file. The email is sent successfully according to my standards. However, I want t ...

Examining Resolver Functionality within NestJS

Today I am diving into the world of writing tests for NestJs resolvers. I have already written tests for my services, but now it's time to tackle testing resolvers. However, I realized that there is a lack of documentation on testing resolvers in the ...

Obtain the height and width of the original images from the href and assign them to your attributes using jQuery

Hey there pals, I'm currently on a mission to fetch the dimensions (height and width) of an image from a hyperlink and then insert those values into its attribute. This task has got me going bonkers! Here's my snippet: <figure> <a ...

Determine the employees' salaries and display any salaries that are below 5000

Hi everyone, I'm looking for guidance on how to properly utilize the Flat or flatMap method to flatten this array of employee data. Specifically, I want to retrieve the names and salaries of employees whose salary is less than 5000. const employeeData ...

How to run 'npm' scripts externally in package.json on Windows?

In the world of development, running arbitrary commands using npm run is a common practice. This involves adding a scripts hash to your package.json: "scripts": { "build-js": "browserify browser/main.js | uglifyjs -mc > static/bundle.js" } To exec ...

Issue finding a route based on dates

Hey everyone, I'm working on a feature where I need to fetch tasks made by a user within a specific date range provided by the user. However, I am facing some issues with getting the date and routing it. Here is the URL format that I am trying to work ...

Is javascript or ajax the best choice for updating a database in asp.net mvc?

I need help with updating a row in my database based on a change event from a dropdown list. I am not sure whether to use javascript or ajax for this purpose as I want to avoid page refresh. Any recommendations on which method is best and where I can find ...

Enhance your Rails application by dynamically updating table cells with Ajax, eliminating the need for

In my index.html.erb file, I have set up one form and one table. Using Ajax allows me to submit the form without any redirects. <%= form_for approval, remote: true do |f| %> <%= f.check_box :pass %> <%= f.submit%> <% end %> My ...

Having trouble utilizing Reactjs Pagination to navigate through the data

I'm currently working on implementing pagination for a list of 50 records, but I'm encountering an issue. Even though I have the code below, it only displays 10 records and I'm unaware of how to show the next set of 10 records until all 50 a ...

Combining two HTML5 videos from Youtube within a single webpage with the assistance of Google Image proxy

Currently, I am utilizing this JavaScript code to import a Youtube video and output it as HTML5. The code works perfectly fine, but now I am interested in having two videos on the same page. Is there a way to modify this code to accommodate two different v ...

What's the best way to display my slider exclusively on the homepage?

Hello, I am experiencing an issue with my slider. I would like to display the slider only on the home page and not on any other sub-pages. Is there a way to achieve this? Below is the code that I am using: <script> $( ...

Is there a way to display a specific dropdown menu depending on the checkbox that is selected?

I have a bunch of checkbox items, including one labeled nocalls, as well as a couple of dropdownlist boxes. Here are the dropdown boxes: <tr> <td align="right"><FONT class="Arial10"><B>Profile<font color="#ff0000">*</ ...

Guide to triggering an API call upon changing the value in a Vue Multiselect component

Is there a way to trigger an API call when the value changes in a Vue Multiselect component? I want to update the value in the multiselect, make an API call, and display the result in the console. Below is my code snippet. <template> <div> ...

Commencing CSS Animation Post Full Page Loading

I am looking for a solution using WordPress. In my specific case, I want the CSS Animations to take effect only after the page has completely loaded. For example: click here This is my HTML: <div class="svg-one"> <svg xmlns="ht ...