I'm trying to load TypeKit fonts into an instance of the CKEditor via jQuery. Here's my code:
$('.ck-blog textarea').ckeditor(function () {
CKEDITOR.scriptLoader.load(
"http://use.typekit.com/zku8fnp.js",
function (success) {
Typekit.load();
alert(success); },
null,
true);
},
{
resize_enabled: false,
skin: 'kama',
height: '500px',
toolbarCanCollapse: false,
toolbar_Full: toolbar,
forcePasteAsPlainText: true,
autoGrow_onStartup: true,
templates_replaceContent: false,
extraPlugins: 'autogrow,wordcount',
removePlugins: 'resize',
contentsCss: '/areas/admin/content/css/ckeditor-blog.css',
templates_files: ['/areas/admin/scripts/ckeditor-templates.js'],
autoParagraph: false
});
I'm getting the success alert after TypeKit is supposed to load but I am not see the fonts load. Any idea what I may be doing wrong?
CKEDITOR.scriptLoader
appears to be used to load assets to the parent window, not into the iframe'ddocument
. This causes your code above to execute and re-apply the Typekit font to the parent window instead of the iframe.My solution for this is to set
document.domain
in your parent window, create<script>
elements dynamically, append them to the<head>
of the iframedocument
.1. Your Typekit has a domain whitelist. CKEditor will not set set the
<iframe>
tag'ssrc
attribute to return a valid value in this whitelist unless you specifydocument.domain
in your parent window to be a domain on that whitelist.2. I created the scripts with lines like
3. I then append these to the dom (I use jQuery in my project, so that's how I target the element)
The Typekit fonts are now applied.
Modified for use within the CKEditor setup
Maybe there is a better way than the
setTimeout(function(){ ... }, 0);
(a 0ms delay), but leaving just thetry{Typekit.load();}catch(e){}
does not givescript1
enough time to be appended and interpreted by the browser to begin blocking. Also note, my use ofsprintf()
above comes from a library (not native JS).Here what I've managed to assemble after 3 hours of digging over Internet:
Trick here is to use CKEditor's "window" object, that comes from iframe.