Jump to content

contentWindow or contentDocument


david77
 Share

Recommended Posts

Can someone please explain this to me from http://www.w3schools...ntentwindow.asp What is x?What is y?What does "if (y.document)y=y.document;" mean? function changeStyle(){var x=document.getElementById("myframe");var y=(x.contentWindow || x.contentDocument);if (y.document)y=y.document;y.body.style.backgroundColor="#0000ff";} fnlights() is called by clicking on the L button of my 'cpu' on http://homepage.ntlw...andtraffic2.htm function fnlights() {var lite, attrgoslow=false;var doc=top.document.getElementById("trafficdoc");var obj=(doc.contentWindow || doc.contentDocument);if (obj.document) obj=obj.document;lite = obj.getElementById("x1red");attr = lite.getAttribute("fill");if (attr == "red") { lite.setAttribute("fill", "black"); }else { lite.setAttribute("fill", "red"); }} This should toggle the top left red light. It works on Opera and Firefox but not on IE8 or Chrome browsers. Advice much appreciated, thanks Bring back Pascal, all is forgiven!

Link to comment
Share on other sites

IE OR old versions of IE (not sure about IE9 if this still applies) uses a different method of accessing iframe as in .contentWindow, while other better browsers use the other. only .contentWindow will use '.document' as in .contentWindow.document To determine which method to use for specific browser we find out which one it recognises var obj=(doc.contentWindow || doc.contentDocument); //use method that browser will support if (obj.document) // does the method you support use .document, if yes the selected method will apply .documentobj=obj.document; Edit: instead of 'top' try 'parent' instead var doc=parent.document.getElementById("trafficdoc");

Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

I fear that you are right, dsonesuk. IE8 displays the CPU and Traffic light images, but it looks as tho I can't get the cpu to control the lights. I will detect IE8 and show an alert: "Sorry - my prog doesn't display with that browser. Try Opera, Firefox, or Chrome." Thanks again.

Link to comment
Share on other sites

My next problem is getting the cpu to write to the traffic lights. When I was getting the traffic lights to work, I had test buttons to call function fnportone(opcount) or fnporttwo(opcount) to change the lights. If you click on N to load a new prog, a child.htm page comes up with a preloaded simple prog. Clicking on Paste to cpu on the child page calls window.opener.storeprog(s); in the child page to store the prog in CPU memory. I was hoping to do something like that from the cpu to call the traffic functions.

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
 Share

×
×
  • Create New...