script problem

S

stach

Hi this is my fragment of code:
---------------------------------------------
function addPub(){
var d = document.getElementsByClassName('post-body');
for(var i=0;i<d.length;i++){

/* find and replace word in code */

d.innerHTML = d.innerHTML.replace(/ page /g, ' <a class="vink"
href="./page.html" target="_top">page</a> ');

}
}

function addLE(func) {

var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}

addLE(addPub);
 
D

Dr J R Stockton

In comp.lang.javascript message <3c9a1e1e-4ab0-4255-9998-90f507f53fa6@k1
7g2000vbn.googlegroups.com>, Wed, 15 Jun 2011 15:43:17, stach
problem its if world page have url exmple <a href="#top">page</a> code
crash :( how to block serach in some type <a <textarea <code etc....

Consider whether you can do this :
Firstly, replace all instances of 'page' that are within <a <textarea
<code etc.... by some word or character that does not appear anywhere in
the page. Secondly, perform your substitution for 'page'. Thirdly,
undo firstly.

For most of us, \u1024 is unlikely to appear; and \uABBA more so. Or
\uBEEF, \uFEED. All four in sequence must be highly improbable.
 
J

Jukka K. Korpela

The original question was rather obscure, but I think we can deduce that
the OP wants to change all occurrences of the word "page" to links if
they occur inside an element that belongs to a specific class. And he
wonders what to do if the word is already a link, or inside a link, or
inside <textarea> or <code> or... something.

I wonder what "crash" means here. I don't see why anything would crash
if you turn a word inside <code> into a link, for example.

Anyway, the proper way is to traverse the document tree, or a suitable
branch thereof, and perform the operation on text nodes only. There you
can add checks that exclude what you want to exclude, e.g. so that when
recursive traversing a branch, you skip an element if its tagName is in
your exclusion list.

That way, you won't accidentally change "page" to a link when it appears
inside an attribute value like title="page" or href="page".
Consider whether you can do this :
Firstly, replace all instances of 'page' that are within<a<textarea
<code etc.... by some word or character that does not appear anywhere in
the page. Secondly, perform your substitution for 'page'. Thirdly,
undo firstly.

That's trickery, and not even elegant trickery.
For most of us, \u1024 is unlikely to appear;

If I wanted to use characters that "can't appear", I wouldn't start from
letters of any language, even a small language that is only spoken by
about 40 million people.

Rather, I would use characters that aren't characters, i.e. noncharacter
code points, which are characters in the JavaScript meaning of the word
but not by the character standards. That way, I would only risk messing
up documents that were already messed up
 
E

Elegie

Le 17/06/2011 07:05, Jukka K. Korpela a écrit :

Hi,
I wonder what "crash" means here. I don't see why anything would crash
if you turn a word inside <code> into a link, for example.

I seem to remember something about generating links into TEXTAREA, and
having it crashing IE. However, that was years ago, so unfortunately I
cannot be more specific (what browser version? Had it something to do
with the HTML-based edition mode, or Ranges, or simple DOM-methods?).

Anyway, the following still gives an interesting result in my IE9. Can
you guess the output? :)

---
<form>
<textarea cols="10" rows="20"></textarea>
</form>

<script type="text/javascript">
var
t=document.forms[0].elements[0],
a=document.createElement("a"),
x=document.createTextNode("Hello, World!");

a.href="http://www.wikipedia.org/";
a.appendChild(x);
t.appendChild(a);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top