Reply
Old July 28th, 2004 Top | #1
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default [javascript] document.write() doesn't write!

I was bored, so I decided to make a javascript which replaces words while typing in a textfield. But I also wanted a counter with it, so I used document.write('Replaced words so far ' + counter), where counter is added up each time a word is replaced. The problem is, it doesn't write, not even the text-bit. Here's the code:
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>
IE (I am at work now, I can't test it with another browser) doesn't give a javascript error, the text is being replaced, yet it doesn't write a thing. I even tried to place a document.write('test'); within the last script brackets, but that doesn't work also, can someone help me?
Glaanieboy is offline   Reply With Quote
Old July 28th, 2004 Top | #2
 
Nick's Avatar
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102

Default

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.
Nick is offline   Reply With Quote
Old July 28th, 2004 Top | #3
 
Nick's Avatar
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102

Default

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.
Nick is offline   Reply With Quote
Old July 28th, 2004 Top | #4
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default

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?
Glaanieboy is offline   Reply With Quote
Old July 28th, 2004 Top | #5
 
Nick's Avatar
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102

Default

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>
Nick is offline   Reply With Quote
Old July 28th, 2004 Top | #6
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default

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>
Thanks for the code though, I didn't know how to get the HTML value, now I know
Glaanieboy is offline   Reply With Quote
Old July 28th, 2004 Top | #7
 
Nick's Avatar
OSNN Lurker
Joined: February 2004
Location: North West, UK
Posts: 147
Reputation: 90
Power: 102

Default

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.
Nick is offline   Reply With Quote
Old July 28th, 2004 Top | #8
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default

It's a very handy feature
Glaanieboy is offline   Reply With Quote
Old July 28th, 2004 Top | #9
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default

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>
Glaanieboy is offline   Reply With Quote
Old July 28th, 2004 Top | #10

OSNN Folding Team  
Khayman's Avatar
I'm sorry Hal...
Joined: January 2002
Location: England
Posts: 5,514
Reputation: 1210
Power: 194

Default

nice code, but that fetaure is built in to vBulletin

"*I'm* on the server side. I don't know what side you're on"
Khayman is offline   Reply With Quote
Old July 28th, 2004 Top | #11
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default

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!
Glaanieboy is offline   Reply With Quote
Old July 28th, 2004 Top | #12

OSNN Folding Team  
Khayman's Avatar
I'm sorry Hal...
Joined: January 2002
Location: England
Posts: 5,514
Reputation: 1210
Power: 194

Default

unless I disable java script

"*I'm* on the server side. I don't know what side you're on"
Khayman is offline   Reply With Quote
Old July 28th, 2004 Top | #13
 
Glaanieboy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: The Netherlands
Posts: 2,626
Reputation: 270
Power: 153

Default

True. But you can just think of this as an extra function, using PHP as a backup for things like this.
Glaanieboy is offline   Reply With Quote

Reply

Bookmarks

Thread Tools

Posting Rules

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