Skip to content Skip to sidebar Skip to footer

Javascript Runtime Error: Unable To Add Dynamic Content

I'm making a javascript metro app and have some code like this: and when I tried to ru

Solution 1:

Windows 8 restricts the content you can set through innerHTML and Writeln, because it's considered unsafe...

The correct way to add content is:

// The untrusted data contains unsafe dynamic contentvar unTrustedData = "<img src='http://www.contoso.com/logo.jpg' on-click='calltoUnsafeCode();'/>";

// Safe dynamic content can be added to the DOM without introducing errorsvar safeData = window.toStaticHTML(unTrustedData);

// The content of the data is now // "<img src='http://www.contoso.com/logo.jpg'/>" // and is safe to add because it was filtereddocument.write(safeData);

If your code has some javascript, you can use this function (But microsoft dont recomend it):

MSApp.execUnsafeLocalFunction(function() {
    var body = document.getElementsByTagName('body')[0];
    body.innerHTML = '<div style="color:' + textColor + '">example</div>';
});

See at:

http://msdn.microsoft.com/en-us/library/windows/apps/Hh767331.aspx

For your case:

MSApp.execUnsafeLocalFunction(function() {
    document.writeln(foo());
});

Note that you should only do this if you understand your content is safe; if you don't, I would recommend using the toStaticHTML method.

Solution 2:

regarding to the docs I would try :

document.writeln(window.toStaticHTML(foo()));

Solution 3:

Windows 8 store apps have a restriction on placing dynamic content inside innerHTML attribute. To fix this you need to include winstore-jscompat.js file from following location in your page as first reference. Please see this link to know more about winstore-jscompat.

This file is not required on Windows 10.

Post a Comment for "Javascript Runtime Error: Unable To Add Dynamic Content"