Specify Samesite when embedding a YouTube video using JavaScript

Hello there!

I’ve been trying to embed a YouTube video on my website using JavaScript using the method on the website here: YouTube Player API Reference for iframe Embeds

However, this code causes an issue to show up in the Issues panel. "Because a cookie’s SameSite attribute was not set or is invalid, it defaults to SameSite=Lax, which prevents the cookie from being set in a cross-site context. This behavior protects user data from accidentally leaking to third parties and cross-site request forgery.
Resolve this issue by updating the attributes of the cookie:
Specify SameSite=None and Secure if the cookie is intended to be set in cross-site contexts. Note that only cookies sent over HTTPS may use the Secure attribute.
Specify SameSite=Strict or SameSite=Lax if the cookie should not be set by cross-site requests.
How would you do this?

Hey there!
Help me understand better, so maybe I can help you, why are you trying to embed a video into your website using JS instead of html?

My advise, in case you’d want to use JS only to delay the network request (which is smart, because network requests to Google unfortunately weight a lot in terms of performances) would be to simply use Element.setAttribute() to an empty div/iframe you already have set up in your html. With JS you could technically add the element out of the blue all together if you want, but in my opinion it may be a little bit more stressful the styling part.

However this solution may not be viable if your element is part of the First Meaningful Paint of your webpage, because delaying it with JS would be bad for performances. :slight_smile:

Thanks for the help!

1 Like