Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21297 > unrolled thread

Re: Can't refer to innerHTML contents

Started byEdmund Maruhn <ema@outermedia.biz>
First post2013-09-12 08:53 -0700
Last post2013-09-12 20:31 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Can't refer to innerHTML contents Edmund Maruhn <ema@outermedia.biz> - 2013-09-12 08:53 -0700
    Re: Can't refer to innerHTML contents Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-12 18:24 +0200
      Re: Can't refer to innerHTML contents "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-12 20:31 +0200

#21297 — Re: Can't refer to innerHTML contents

FromEdmund Maruhn <ema@outermedia.biz>
Date2013-09-12 08:53 -0700
SubjectRe: Can't refer to innerHTML contents
Message-ID<c4fc8bad-5a37-4cb7-aa93-34dc849382b0@googlegroups.com>
A bit old this thread but someone may find this useful.
Today I found the loadHTMLFragment function here and tried to add it into a project. After integration an exception was raised by the function when it attempts to remove the old script children.
I tweaked it a bit and now it has two improvements:

1. The exception has gone
2. Each script element becomes integrated at the exact same place as the old one was placed in the structure. Means you will get the same sub-tree as before. The previous version (at the beginning of the thread) added all scripts at the end of the element indicated by the element-id passed in to the function.

Maybe this has been solved already by someone before my post here. However, here is my solution:

function loadHTMLFragment(elementId, fragment)
	{
		if (document &&
			document.getElementById &&
			document.getElementById(elementId) &&
			document.createElement &&
			document.appendChild &&
			document.getElementsByTagName) {

			var element = document.getElementById(elementId)
			// &nbsp; forces IE to process script elements if the first node
			// is a script element
			element.innerHTML = "&nbsp;" + fragment;
			
			
			var scriptChildren = element.getElementsByTagName('script');
			var length = scriptChildren.length;
			var scripts = [],
				newScript,
				oldScript;
			
			for (var scriptIndex = 0; scriptIndex < length; ++scriptIndex) {
				oldScript = scriptChildren[scriptIndex];
				
				newScript = document.createElement('script');
				newScript.type = "text/javascript";
				newScript.text = oldScript.text;
				scripts.push({
					oldScript: oldScript,
					newScript: newScript
				});
			}
			/*
			function getChildIndex(child) {
				var index = 1;
				while (node = node.previousSibling) {
					if (node.nodeType === 1) {
						index++;
					}
				}
				return index;
			}
			*/
			var parent,
				current,
				before,
				length = scripts.length;
			
			for (var scriptIndex = 0; scriptIndex < length; ++scriptIndex) {
				current = scripts[scriptIndex];
				parent = current.oldScript.parentNode;
				before = current.oldScript.nextSibling;
				parent.removeChild(current.oldScript);
				parent.insertBefore(current.newScript, before);
			}
		}
	}

[toc] | [next] | [standalone]


#21300

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-09-12 18:24 +0200
Message-ID<1676242.YrdhhZhtd8@PointedEars.de>
In reply to#21297
Edmund Maruhn wrote:

> A bit old this thread

“A bit old” indeed – it is from 2007, so hardly relevant in 2013.  A lot has 
changed in the meantime.  Only Google Groups users would see it because 
Google Groups is (based on) a Usenet archive, and it is FUBAR on top of 
that.  Please either do not use it or tell Google to fix it.  TIA.

> […]
> Maybe this has been solved already by someone before my post here.

Maybe, during those 6 years.

> However, here is my solution:
> 
> function loadHTMLFragment(elementId, fragment)
> {
> if (document &&
> document.getElementById &&

This (type-converting a host object's method) will break in not that recent 
IE/MSHTMLs.

> document.getElementById(elementId) &&
> document.createElement &&

Same here.

> document.appendChild &&
> document.getElementsByTagName) {

And here.  Consider jsx.object.isHostMethod() and the like instead:

<http://PointedEars.de/wsvn/JSX/trunk/object.js>

Your code does not appear to solve a real problem, let alone the unreal 
problem the OP had 6 years(!) ago.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#21301

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-09-12 20:31 +0200
Message-ID<XnsA239D0D124DB2eejj99@194.109.133.133>
In reply to#21300
Thomas 'PointedEars' Lahn wrote on 12 sep 2013 in comp.lang.javascript:

> Your code does not appear to solve a real problem, let alone the unreal 
> problem the OP had 6 years(!) ago.

if (Javascript && beginnersLuck) 
   alert('Works!') 
else
   alert('Not!'); 

// ;-(

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web