Click to See Complete Forum and Search --> : problems with if


scragar
06-21-2003, 05:36 AM
I am having many probllems with the if santax.
Any1 wanna help by writing a standard If sanatax down then I can look through it for my mistake? (I am on a public computer and the edit menu has been disabled by the server so I cannot do it alone?!)


P.S. anyone know how to get JS to search a variables second or third letter for a term?

Charles
06-21-2003, 06:27 AM
See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/stmt.html#1004833 and http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/regexp.html#1194128.

<script type="text/javascript">
<!--
String.prototype.find = function (term) {return new RegExp ('^.{1,2}' + term, 'i').test(this)};

alert ('myFoo'.find('foo'));
// -->
</script>

scragar
06-21-2003, 08:34 AM
I have several Idea's as to what my problems are:

Can you leave an enter after opening script?
do you have to use: <script="javascript"> or will <script> do?
Must if be in <script> and if so can I put it in the same <script> as my prompt?

P.S. please don't quote me I don't like it, and don't redirct me back to that page, It doesn't help and I hate it!!!

Charles
06-21-2003, 08:40 AM
Originally posted by scragar
do you have to use: <script="javascript"> or will <script> do? Neither:

<script type="text/javascript">

Charles
06-21-2003, 08:53 AM
The "language" attribute was depricated back in 1997 and you would have to go way back in time to find a browser that doesn't understand the "type" attribute. And when you consider that 13% of browsers currently do not use JavaScript at all, it's not worth the extra typing.

Also, some versions of Netscape will behave in some non-standard ways if you include the "language" attribute.

scragar
06-21-2003, 09:02 AM
how do you end:
<script language="javascript" type="text/javascript">

is it just standard:
</script> or must you use something else?

Would some1 please make a santax that works and put it up here (with the tags) then I can paste it into my page and modifie it myself (and check the thing works, I have not had it working yet)!!!!!!!!!!!!!!!!!!
any help apreciated.
thanks to all who help.

Charles
06-21-2003, 09:05 AM
<script type="text/javascript">
<!--
String.prototype.find = function (term) {return new RegExp ('^.{1,2}' + term, 'i').test(this)};

alert ('myFoo'.find('foo'));
// -->
</script>

What exactly is it that you are trying to accomplish? And how will you know when you have done it?

scragar
06-24-2003, 05:08 AM
I want a site that call's the visitor's by name, I know I can do this normal using documentwrite, but I wish the site to be mopre original and perhaps have a choice (determined by a random number, tested with if) of different responces. the site should also be able to filter out swear word's and other such term's.

Charles
06-24-2003, 06:10 AM
That tells me "in general" what you are up to, so my comments will be "in general".

1) Keep in mind that about 13% of users do not use JavaScript and that some of those good people cannot use JavaScript because of disabilities. You need to ensure that your page works when there is no JavaScript. If this is fluff, as it appears that it is, then make good use of the NOSCRIPT element. Otherwise do your scripting server side.

2) The "if" statement is not the way to implement a "random quote" script. You would do much better to randomly access the elements of an array.

<script type="text/javascript">
<!--
// First, we'll create an anonymous Array and assign it to "randomQuotes".
randomQuote = ['fee', 'fie', 'foe', 'fum'];

// In JavaScript Arrays are objects so we can give "randomQuote" its own "toString()" method that will return one element of the Array selected randomly.
randomQuote.toString = function () {return this[Math.round(Math.random() * 100) % this.length]}

// Now whenever we use "randomQuote" as if it were a string it will become just a random quote.
alert (randomQuote);

// But, we can still treat "randomQuote" like an Array if we need to.
randomQuote.push('Giant Says');
alert(randomQuote);
// -->
</script>

Charles
06-24-2003, 06:17 AM
<script type="text/javascript">
<!--
String.naughtyWords = ['fee', 'fie', 'foe', 'fum'];

String.prototype.cleanUp = function () {s = this; for (i=0; i<String.naughtyWords.length; i++) {s = s.replace(new RegExp(String.naughtyWords[i], 'ig'), '*')}; return s}

alert (String.naughtyWords.toString().cleanUp())
// -->
</script>

David Harrison
06-24-2003, 05:04 PM
Two things:

* If you use language="javascript" you can't validate your code as strict (I'm not sure about trans).

* I'm sure that Charles has helped you out a great deal, however he hasn't explained how if/else statements work, and instead has launched right into more complex code. Come on, not everyone's an expert you know.

Anyway here's how if/else statements work explained (badly).

This is a simple lot of if/else statements to redirect users to different pages based on a random number.

var rand = Math.random()

if(rand>0.8){location.href="8.html";} // Not much to explain here, it's almost valid english.

else if(rand>0.2){location.href="2.html";} // locaton.href will only equal 2.html if the first statement is not true and this statement is true.

else{location.href="default.html";} // If, and only if, all other statements are false will the location.href equal default.html, you don't need to have this final else statement if you don't want, it just means that if rand<=0.2 nothing will happen.

I hope this helps you out a little, and I'm sorry if I've upset Charles in any way, that wasn't my intention.

scragar
07-01-2003, 06:19 AM
Thank's for all the help, and sorry if I have made any problem's but it's still failing to work regardless.
could ther problem be when I set the variable via a prompt?
I have not had a chance to check this fully but the prompt doesn't come up so I think it could be that?!

P.S. I thought of producing an RPG, I have all the info and I was thinking of doing it one this work's out, any tips?

scragar
07-02-2003, 04:49 AM
this is what i'm using from echoecho.com
username=prompt("Please enter your name","Enter your name here");
but it doesn't appear, and there is no point testing it if the prompt doesn't appear to test!

jeffmott
07-02-2003, 06:31 AM
If there is a syntax error somewhere in the script, browsers will not be able to execute any code that is within the same SCRIPT element that the error occured. If you used the prompt statement by itself (at least until the real error is tracked down), it should work.

scragar
07-04-2003, 03:52 AM
I have discovered the mistake.
echoecho.com never said you must first identife that it is a variable.
it's amazing what differance:
var username;
var username;
makes don't you think?

anyway, that is done. I am now atempting to create a game, and I need to have JS return to line 78 of the HTML, is there any way I can do this?
P.S. no-one has yet said if it was possible to search the 2nd or 3rd digit of the username, any-guesses apreciated.

scragar
07-07-2003, 06:53 AM
my computer just stoped working the script, all I did was change the if santax and now it won't work regardless of what I do, and no the little triangle doesn't appear.

scragar
07-08-2003, 06:14 AM
<script type="text/javascript">
var username
username=prompt("enter your name","")
if (username==hi)
{alert("that's not a name")
document.write("try using a real name")
};
else {
alert("hello "+usename+"!")
document.write("hello "+usename+", I hope you enjoy the site")
};
</script>

I think I must have made another error somewhere.

scragar
07-10-2003, 04:09 AM
thank's.

is it posible to load a new page directly from JS?
what I mean is an alert pop's up, then they click OK and it forward's them?
any idea's?
What does <noscript> do?

scragar
07-10-2003, 08:54 AM
How do you run a function?
I am working on an RPG and I wish to have the function activate to run the santax again, but I know how to create one, just not how to run them.
I apreciate all help thet's been given so-far.

and I never said I was good with HTML just that I like working in it.