Jump to content

Couple o' Questions


Chocolate570

Recommended Posts

1. Ok, so to cut a long story short, i'm making a script that will only work if run at the body onunload event. The problem: I don't have access to change the body tag. How do I change the onunload (if that's what it is) event from a javascript function? window.onUnload=""? 2. I need to take the value of a text(area?)box and see if it contains one of many phrases. I was thinking of doing a for loop depending on how many phrases could be switched, then put an select case inside that loop to call a specific function depending on what case was pressed. Or maybe just one function with paramaters. But i know that will work; what I need to know is how to figure out if there's a particular word in a variable?3. How do I change the selected index of a option box? :ph34r:4. From question 2, I need to check if there's more than one of the possible phrases in the box. :) How would I do that? :ph34r:5. How do I escape every character out of a string so that I don't have to do each one completely? This is what I mean:O'M'G' T'H'I'S' would have to be O\'M\'G\' T\'H\'I\'S\'Which is quite annoying. Is there a way to auto-escape everything that needs escaping? Thanks in advance.Chocolate570

Link to comment
Share on other sites

I think this page covers everything, as far as the escape, I've read that "escape()"is being depricated so "encodeURI()" might be better for this, included an example of the literal "\" escape, don't know if that is what you require.I tested the "onunload" in IE6 and FF, worked fine.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head>  <title></title><script type="text/javascript">var phrases = new Array('Never say Never','That\'s what I\'m talking about','Don\'t run with scissors');var words = new Array('test', 'data', 'w3schools');var stra = 'O"M"G" T"H"I"S"';var strb = "O'M'G' T'H'I'S'";//this regexp checks for the ' or " in the stringvar re = /('|")/g;alert(encodeURI(stra));alert(escape(stra));//use match here so that the character is remembered//then used in our replacement stringvar m = stra.match(re);str = stra.replace(re,'\\' + RegExp.$1);alert(str);var m = strb.match(re);str = strb.replace(re,'\\' + RegExp.$1);alert(str);function pickSel(){var oSel = document.forms['f1'].elements['s1'];//next line picks active item in selectoSel.selectedIndex = 2;}//this function loops through the phrases as it finds one// in the textarea it increments the variable "occurs"function testPhrase(s){var occurs = 0;  for(var i = 0; i < phrases.length; i++){    var re = new RegExp(phrases[i],"i");    if(s.match(re)){      occurs++;      }  }alert('Matches found: ' + occurs);}// loops thru words array tests value of word textareafunction testWords(s){var occurs = 0;  for(var i = 0; i < words.length; i++){    var re = new RegExp(words[i],"i");    if(s.match(re)){      occurs++;      }  }alert('Matches found: ' + occurs);}window.onunload = function() { alert('test') };</script>  </head><body onload="pickSel()"><form name="f1">  <select name="s1">   <option value="">Pick One   <option value="1">First Item   <option value="2">Second Item  </select>  <h3>Phrase Text Box:</h3>  <textarea name="ta1" rows="3" cols="35" onblur="testPhrase(this.value)">Never say never  </textarea> <br />    <!-- COULD BUILD A FUNCTION TO SCROLL THRU THE PHRASES -->  <input type="button" value="<<"> <input type="button" value=">>">    <h3>List Some Words:(test, data, w3schools)</h3>  <textarea name="ta2" rows="3" cols="35" onblur="testWords(this.value)">test truck car bus data  </textarea> <br /></form></body></html>

The buttons below the textarea are just there so that you could study whether or notyou might want to let the user scroll thru the phrases.Shout back if you want to make them active.Thanks, :)

Link to comment
Share on other sites

Question 1.... AnsweredQuestion 2.... UnansweredQuestion 3.... AnsweredQuestion 4.... UnansweredQuestion 5.... AnsweredThanks so much hacknsack, but do you think you could comment that a bit more? I don't understand what parts do what for questions 2 and 4. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...