Jump to content

Problem with w3IncludeHTML


stevensrmiller

Recommended Posts

I've encountered a problem with the w3IncludeHTML function in http://www.w3schools.com/lib/w3data.js. I've posted a question about it at http://stackoverflow.com/questions/40162907/w3includehtml-sometimes-includes-twice. Most forums discourage reposting the same question in more than one place. What's the protocol here? This community is probably more likely to have folks who can help with my issue, but I don't want to start off with poor manners. Please let me know how best to ask for help.

 

Thanks!

Link to comment
Share on other sites

I pulled out the source from w3schools' JavaScript file, and made a change that seems to make the problem go away. But it's not a lasting fix as it relies on a deprecated operation. I hope someone here can have a look at the stackoverflow link, above, and help me understand that I'm seeing.

Link to comment
Share on other sites

Do you ever see the same symptom when you run the examples?

 

Good question. No, it behaves properly in the Tryit Editor. Now, maybe Tryit introduces some critical difference over how things run when the browser is getting its HTML directly from the server. When you click "Run" in Tryit, you don't communicate with the server (I think). Instead, you run this JavaScript:

 

submitTryit()
//var adrefreshtimer
function submitTryit(n) {
  var text = document.getElementById("textareaCode").value;
  var ifr = document.createElement("iframe");
  ifr.setAttribute("frameborder", "0");
  ifr.setAttribute("id", "iframeResult");  
  document.getElementById("iframewrapper").innerHTML = "";
  document.getElementById("iframewrapper").appendChild(ifr);
  var ifrw = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
  ifrw.document.open();
  ifrw.document.write(text);  
  ifrw.document.close();
  //23.02.2016: contentEditable is set to true, to fix text-selection (bug) in firefox.
  //(and back to false to prevent the content from being editable)
  //(To reproduce the error: Select text in the result window with, and without, the contentEditable statements below.)  
  if (ifrw.document.body && !ifrw.document.body.isContentEditable) {
    ifrw.document.body.contentEditable = true;
    ifrw.document.body.contentEditable = false;
  }
//  if (!adrefreshtimer) {adrefreshtimer = new Date().getTime();}
//  if (new Date().getTime() - adrefreshtimer >= 30000) {
//  if (n == 1) {
//    googletag.cmd.push(function() {
//      googletag.pubads().refresh([gptAdSlots[0]]);
//    });
//  }
//    adrefreshtimer = new Date().getTime();
//  }
}
I'm a JavaScript noob, as yet, but it looks to me like this line reads the JavaScript in the code window:
  var text = document.getElementById("textareaCode").value;
And it looks to me like this line writes that JavaScript to the result window:
  ifrw.document.write(text);
That would appear to be a purely synchronous operation, whereas I am guessing (and just guessing, mind you :) ) that this problem arises when the include is executed asynchronously with transmissions from the server. W3's code looks like it tries to handle that with this line in their function:
if (xhttp.readyState == 4 && xhttp.status == 200)
as I believe that hard-coded 4 tests for the page being done loading. So, it ought to work. And it does in most browswers, and for most pages. The only time I see a problem is in Chrome, with a really short page. That just smells like a synchronization error, to me.
What's your take?
Edited by stevensrmiller
Link to comment
Share on other sites

That is because its using that code to read the inserted code from textarea element from left to a iframe on right.

So I was right! Thanks.

 

Lets start from beginning

1) What editor are you using?

Notepad.

Link to comment
Share on other sites

Try by downloading Notepad++, open and save all( may require making slight change to enable a save) files in this program, then test.

Thanks for the suggestions. That avenue doesn't look promising, to me. Maybe someone here could test my code and see if they encounter the same problem?

Link to comment
Share on other sites

I have tested the code, and work without problems in ALL browsers. Notepad++ is popular editor, and save files, by default as encoded utf8 for website.

Well, then let's see if the problem is really in my files. Would you please try this page in a Chrome browser and reload it a few times? When it behaves properly, the output looks like this:

 

« PreviousHome

  • First
  • Second

But, when it behaves incorrectly, the output looks like this:

« Previous

  • First
  • Second
  • First
  • Second

Can you make the error happen?

Link to comment
Share on other sites

Ah, I think I found the problem. I have "Chrome Stylist" installed in my extensions. Disabling it made the problem go away.

 

Thanks for your time and effort. Much appreciated!

Link to comment
Share on other sites


 

I see the issue in Opera. Your page is missing a doctype and top-level HTML tag, I would start by adding those and making it a valid document.

 

 

 


 

You also need to add doctype and html tag

<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> </body> </html>

 

 

Added the doctype and html tags. No change in behavior. The problem still comes and goes depending upon whether or not the Chrome Stylist extension is enabled or not.

 

justsomeguy, do you have any extensions that intercept server output enabled?

 

No need to keep working on this, folks. Stylist hasn't been updated in three years, and I can happily live without it. It's clearly a bug in that code, and I'm not eager to pursue it for them.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...