locked
Unable to Add Dynamic Content, even though toStaticHTML was used!

    Question

  • Hello,

    I am trying to add content from a webpage to my JavaScript app, using the following code:

     WinJS.xhr({ url: "http://xkcd.com/archive/" }).done(
                function onComplete(res) {
                    var arch = res.response;
                    var test = document.createElement("html");
                    test.innerHTML = toStaticHTML(arch.substr(39));
    });

    However, I get a "Unable to add dynamic content" error, even though toStaticHTML() was used. I also tried using
    var test = document.createElement(toStaticHTML(arch.substr(39)))

    but I got an InvalidCharacterError. Is this some sort of caching issue or is it my logic???
    All help is greatly appreciated and I always accept an answer!
    • Edited by sddhhanover Friday, August 31, 2012 11:43 PM
    Friday, August 31, 2012 11:43 PM

Answers

  • var test = document.createElement(toStaticHTML(arch.substr(39)))

    I'm not sure that this particular statement is valid. According to the documentation, the parameter to createElement should be a tag name (like "div" or "a" or "input" for example). Passing the entire contents of an html page probably would contain some characters that would be deemed invalid in a tag name. You may want to take a look at the documentation for create element (http://msdn.microsoft.com/en-us/library/windows/apps/hh465797.aspx).

    If you plan on displaying the archive page as you would see it in a web browser, you may want to consider using an iframe and pointing it to the archive url.



    If you are really determined to set the innerHTML of test, then you could narrow down what part of the html is causing the error by using the debugger and set a breakpoint at the line for setting the innerHTML. Then, have it try to assign smaller and smaller fragments of the static html for arch.substr(39).
    Tuesday, September 04, 2012 4:27 AM

All replies


  • Hi

    As far as I know , you can create a div, then use this code instead:

     WinJS.xhr({ url: "http://xkcd.com/archive/" }).done(
                function onComplete(res) {
                    var arch = res.response;
                    var test = document.getElementById("div1");
                    test.innerText = toStaticHTML(arch.substr(39));
    });

    That will work fine.

    • Edited by Dino He Monday, September 03, 2012 1:54 PM
    Monday, September 03, 2012 1:31 PM
  • test.innerText does not work because it turns the HTML from the webpage into raw text. Thus, the content is not added to my app, just a string representing the content.
    • Edited by sddhhanover Monday, September 03, 2012 10:42 PM
    Monday, September 03, 2012 10:42 PM
  • var test = document.createElement(toStaticHTML(arch.substr(39)))

    I'm not sure that this particular statement is valid. According to the documentation, the parameter to createElement should be a tag name (like "div" or "a" or "input" for example). Passing the entire contents of an html page probably would contain some characters that would be deemed invalid in a tag name. You may want to take a look at the documentation for create element (http://msdn.microsoft.com/en-us/library/windows/apps/hh465797.aspx).

    If you plan on displaying the archive page as you would see it in a web browser, you may want to consider using an iframe and pointing it to the archive url.



    If you are really determined to set the innerHTML of test, then you could narrow down what part of the html is causing the error by using the debugger and set a breakpoint at the line for setting the innerHTML. Then, have it try to assign smaller and smaller fragments of the static html for arch.substr(39).
    Tuesday, September 04, 2012 4:27 AM
  • After a second of debugging, I figured out the issue is somewhere in the <head> tag, so I just removed it. It still seems strange, though, that this would happen even after using toStaticHTML(). Does anyone have an idea why?
    Wednesday, September 05, 2012 10:41 PM