Moshe Shaham
Moshe Shaham

Reputation: 15994

Youtube api error with internet explorer 8 compatibility mode

I'm trying to load a youtube video using the api. I copied and pasted the sample code. But I'm getting an error in Internet explorer 8 when compatibility mode is on, and I want to force 8 mode.

this is the error:

Message: 'JSON' is undefined
Line: 33
Char: 136
Code: 0
URI: http://s.ytimg.com/yt/jsbin/www-embed_core_module-vflDULhso.js

this is the html that has the sample code + forcing mode 8:

http://cdn.radicalislam.org/enriched/test.html

Upvotes: 2

Views: 1964

Answers (2)

Tim Wintle
Tim Wintle

Reputation: 2433

The YouTube iframe API depends on several features which are only provided by IE8+ in standards mode.

These are mentioned under "Requirements" here:

https://developers.google.com/youtube/iframe_api_reference

In particular (in addition to the JSON dependency you mention) there is a requirement on the postMessage API - which cannot be provided by a javascript library. The end result is that the YouTube iframe API cannot be used on IE8 in compatibility mode, or by any other browser which does not support postMessage.

While IE8 does support the requirements in standards mode, dropping to compatibility mode will break the behaviour.

Having looked at the link you posted, you seem to have added a doctype to force standards mode since you posted this - has that fixed the issue?

Upvotes: 2

tungd
tungd

Reputation: 14907

The error is quite surprising as AFAIK JSON support is no longer a problem as almost all browser support it now (http://caniuse.com/json). However it seems that JSON is not supported in your case, so you will need a polyfill such as json2. To load it Modernizr use this:

Modernizr.load({
  test: !!window.JSON && !!JSON.parse,
  nope: 'json2.js'
});

Update: I overlook a note at the bottom of the Can I Use page; to have JSON support in IE8 you will have to use HTML5 doctype as <!DOCTYPE html> in the top of your HTML.

Upvotes: 1

Related Questions