|
|
![]() |
|
|
Top | #1 |
|
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153 |
Code:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function checkField(){
text = document.formname.textfield.value;
var counter = 0;
if (text.search('text') != -1){
text = text.replace('text','bleh');
counter++;
}
document.formname.textfield.value = text;
}
</SCRIPT>
</HEAD>
<BODY>
<script type="text/javascript">
if (!counter OR counter == 0){
counter = 'none';
}
document.write('Replaced words so far: '+ counter);
</script>
<form name="formname">
<input type="text" value="" name="textfield" onKeyup="checkField();">
</form>
</BODY>
</HTML>
|
|
|
|
|
|
Top | #2 |
|
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102 |
One problem is your declaring counter within the function, so it isn't accessable outside the function. If you want to access it globaly, declare it before the function. A better solution might be to return the value you want rather than use counter globally.
I'm not quite sure what the problem is but taking out the if statement before the document.write() makes the document.write work. I'm still trying to figure out why. |
|
|
|
|
|
Top | #3 |
|
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102 |
Found the problem. You should be using || rather than 'OR'. It does exactly the same as or would, the only difference is it has a higher operator precedence.
|
|
|
|
|
|
Top | #4 |
|
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153 |
Still doesn't work. I now get an error (translated from Dutch to English):
Error: Expected object on line 30, char 1 (which is the <input ... thingy) ![]() I also tried making a function (doWrite()) out of the second <script></script>, that was called from the upper script, which in turn brought the counter value (by using doWrite(counter), but still the same error. Is there any freeware javascript/html debugging program available? |
|
|
|
|
|
Top | #5 |
|
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102 |
I haven't come accross any javascript debuggers, doesn't mean there aren't any though.
I think this code will do what your trying to do: Code:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function checkField(){
var counter = 0;
text = document.formname.textfield.value;
if (text.search('text') != -1){
text = text.replace('text','bleh');
counter++;
counterDisp.innerHTML = 'Replaced words so far: ' + counter;
}
document.formname.textfield.value = text;
}
</SCRIPT>
</HEAD>
<BODY>
<div id="counterDisp">Replaced words so far: None</div>
<form name="formname">
<input type="text" value="" name="textfield" onKeyup="checkField();">
</form>
</BODY>
</HTML>
|
|
|
|
|
|
Top | #6 |
|
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153 |
Your counter stops at 1. I modified it a bit to ensure the counter is either set at 0 or at whatever value it has found in the HTML:
Code:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function checkField(){
//check whether the counterDisp value == 'None'
if (counterDisp.innerHTML == 'None'){
//if yes, set counter to 0
var counter = 0;
}else{
//if not, set counter to HTML value (this isn't always true, since 'None' isn't a
//number, so 'None' + 1 results in an error
var counter = counterDisp.innerHTML;
}
text = document.formname.textfield.value;
if (text.search('text') != -1){
text = text.replace('text','bleh');
counter++;
counterDisp.innerHTML = counter;
}
document.formname.textfield.value = text;
}
</SCRIPT>
</HEAD>
<BODY>
Replaced words so far: <span id="counterDisp">None</span>
<form name="formname">
<input type="text" value="" name="textfield" onKeyup="checkField();">
</form>
</BODY>
</HTML>
|
|
|
|
|
|
Top | #7 |
|
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102 |
I made the same misktake I pointed out above, left the counter as a local variable, Doh! If you move it outside the function it works happily.
I've never actually thought of using .innerHTML to read data from a page, I've only ever used it to write back to a page. It's always interesting to see how other people use snippets of code. |
|
|
|
|
|
Top | #8 |
|
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153 |
It's a very handy feature
|
|
|
|
|
|
Top | #9 |
|
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153 |
Here's another addition:
This script checks for bad words which are entered in the textbox and replaces them with ****. These bad words are stored in an array, but can be loaded from a database or sump using PHP. Maybe it can be implemented in this board? Code:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function checkField(){
if (counterDisp.innerHTML == 'None'){
var counter = 0;
}else{
var counter = counterDisp.innerHTML;
}
badWords = new Array('bad1','bad2','bad3');
text = document.formname.textfield.value;
for (var j = 0 ; j < badWords.length ; j ++) {
if (text.search(badWords[j]) != -1){
text = text.replace(badWords[j],'****');
counter++;
counterDisp.innerHTML = counter;
}
}
document.formname.textfield.value = text;
document.formname.textfield.focus();
}
</SCRIPT>
</HEAD>
<BODY>
Replaced words so far: <span id="counterDisp">None</span>
<form name="formname">
<input type="text" value="" name="textfield" onKeyup="checkField();">
</form>
</BODY>
</HTML>
|
|
|
|
|
|
Top | #10 |
|
I'm sorry Hal...
Joined: January 2002
Location: England
Posts: 5,514
Reputation: 1210
Power: 194 |
nice code, but that fetaure is built in to vBulletin
|
|
|
|
|
|
Top | #11 |
|
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153 |
I know, but that first needs parsing in PHP before these words are filtered out. With this javascript, the words are being filtered while typing!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| write protection | harryh31 | Portable Devices & Gadgets | 3 | May 7th, 2007 1:01pm |
| wont write to cd??!?!!?!? | FrenzyD | General Hardware | 17 | April 11th, 2005 9:31am |
| Delayed Write $N | canadian_divx | General Hardware | 0 | June 2nd, 2004 5:47am |
| Write Permission in XP? | Temperal | Windows Desktop Systems | 3 | April 13th, 2003 4:36pm |
| write-caching problem | gvictor1la | General Hardware | 1 | December 28th, 2001 10:45pm |