/    Sign up×
Community /Pin to ProfileBookmark

Strange behaviour within each()

I’m trying to set up a function to process all text nodes within the <body> in two passes:

[CODE]function trv(q,p1){
if(p1){
$(“body *”).contents().each(function() {
if(this.nodeType == 3) {
if(this.data.indexOf(‘!’)!=-1) this.data=”;
}
});
} else {
$(“body *”).contents().each(function() {

if(this.nodeType == 3) { alert(this.data);
if(this.data==”) {
}
}
});
}

}[/CODE]

The strange thing is, that if I place the alert within:

[CODE] if(this.data==”) {
} [/CODE]

…it doesn’t work (on all browsers I’ve tested)

You can see it working here:

[url]http://4nf.org/[/url]

Any ideas why?

Here’s a deep link to the complete respective .js:

[url]http://4nf.org/n.js[/url]

Thanks in advance and kind regards

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@arvgtaauthorJul 18.2012 — Here's the old non-jQuery function "trv" that works:

(see e.g. http://4nf.org/tool/ + http://4nf.org/n2.js)

[CODE]function trv(q,p1){var v2,v3,v4;
v3=q.childNodes;
for(var v=0;v<v3.length;v++)if(v3[v].nodeType==3){
v2=v3[v].data;

if(!p1) {
if(v2.indexOf('!')==-1) continue;
v3[v].data='';
} else {
if(v2!='') continue;
v4=pop1();
if(!v4) continue;
if(v4.charAt(0)=='<') q.replaceChild(bit(v4),v3[v]);
else v3[v].data=v4;
}
} else trv(v3[v], p1);
}[/CODE]


I'm basically trying to turn it into a jQuery version...

Any ideas?

Thanks very much for any kind of support!
Copy linkTweet thisAlerts:
@arvgtaauthorJul 23.2012 — Now got it to work fairly well with the following code:

[CODE]function trv(q) { $("body *").contents().filter(function(){ return this.nodeType == 3; }).each(q); }

function trv0(){ trv(function() {
if(this.nodeValue.indexOf('!')!=-1)
this.nodeValue=' ';
});
}

function trv1(){ trv(function() { var v;
if(this.nodeValue==' ') {
v=pop1();
if(v) {
if(v.charAt(0)=='<') this.parentNode.replaceChild(bit(v),this);
else this.nodeValue=v;
}
}

});
}[/CODE]


...which is much more readable than my old code.

So I would be quite happy with the above if it wasn't for the following problem:

One abbreviation called "Bo header!" is being treated wrong on this page: http://4nf.org/

I think it has to do with this code:

[CODE]this.parentNode.replaceChild(bit(v),this);[/CODE]

What is a better way to write it?

Is the parentNode in scope at all? (I don't know the workings of jQuery)


Thanks in advance and kind regards
×

Success!

Help @arvgta spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...