Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2013, 09:39 PM   PM User | #1
scnerd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
scnerd is an unknown quantity at this point
Cool GM, not getting past if statement

So I the following code is my greasemonkey script:
Code:
var msg = document.body.innerHTML;

if(msg.match(/Big monster/gi).length > 0)
   {
   alert("attack");
   //Do stuff
   }
alert("got past");
if(msg.match(/Big monster/gi).length == 0)
   {
   alert("not attack");
   //Do other stuff
   }
When the first condition is true, the code inside executes perfectly. Otherwise, nothing is getting run. Note that the same thing happens if I use an "else" instead of the second "if". Note too, if the first condition is true, then "got past" gets displayed, but if it's false, then even that line doesn't get executed.

This is my first time with javascript, and also my first time messing with Greasemonkey, so is there something obvious I'm missing here?
scnerd is offline   Reply With Quote
Old 01-31-2013, 09:53 PM   PM User | #2
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
Quote:
Originally Posted by scnerd View Post
but if it's false, then even that line doesn't get executed.
Which indicates that you get an error, for example something is undefined. Use the console to verify what happens.
Airblader is offline   Reply With Quote
Old 01-31-2013, 09:55 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
The length of the value returned will never be zero. When no matches are found the length will be 1.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-31-2013, 10:00 PM   PM User | #4
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
@ felgall

That wouldn't explain the "got past" not being displayed. And it's actually not correct.

Quote:
If the regular expression includes the g flag, the method returns an Array containing all matches. If there were no matches, the method returns null.
https://developer.mozilla.org/en-US/...s/String/match

And as we hopefully all know: null doesn't have a property 'length', thus erroring out.
Airblader is offline   Reply With Quote
Users who have thanked Airblader for this post:
scnerd (01-31-2013)
Old 01-31-2013, 10:01 PM   PM User | #5
scnerd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
scnerd is an unknown quantity at this point
Quote:
Originally Posted by Airblader View Post
Which indicates that you get an error, for example something is undefined. Use the console to verify what happens.
Right, I got that there was an error, but I'm literally running the code above. Besides the conditions and the "alert" calls, I'm actually doing nothing (everything else is commented out), and the behavior is the same. When the condition is met, everything runs, else nothing is run. So my question is more: is there any reason why that first condition being false should mean that the rest of the code doesn't run?

Here, for reference, I am running this *exact* code (this is the entire script):
Code:
var msg = document.body.innerHTML;

if(msg.match(/Big monster/gi).length > 0)
   {
   alert("attack");
   //Do stuff
   }
else
   {
   alert("not attack");
   //Do other stuff
   }
And the behavior is as I said above. Either "attack" gets displayed, or nothing does.
scnerd is offline   Reply With Quote
Old 01-31-2013, 10:02 PM   PM User | #6
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
There is a difference between a condition being false and getting an error.
Airblader is offline   Reply With Quote
Old 01-31-2013, 10:02 PM   PM User | #7
scnerd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
scnerd is an unknown quantity at this point
Quote:
Originally Posted by Airblader View Post
@ felgall

That wouldn't explain the "got past" not being displayed. And it's actually not correct.



https://developer.mozilla.org/en-US/...s/String/match

And as we hopefully all know: null doesn't have a property 'length', thus erroring out.
Thanks, I'd thought that it'd return an empty array, not just null.
scnerd is offline   Reply With Quote
Old 01-31-2013, 10:04 PM   PM User | #8
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
For future reference you should not just learn what match returns from this, more important is to learn that simply looking at the console would've told you exactly the same.
Airblader is offline   Reply With Quote
Old 01-31-2013, 10:07 PM   PM User | #9
scnerd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
scnerd is an unknown quantity at this point
Quote:
Originally Posted by Airblader View Post
For future reference you should not just learn what match returns from this, more important is to learn that simply looking at the console would've told you exactly the same.
For future reference, working in GM, where can I get said console? In firefox, does the Web Dev -> Web Console do what you're talking about? Or does GM have its own debugging console? Thnx
scnerd is offline   Reply With Quote
Old 01-31-2013, 10:10 PM   PM User | #10
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
I have never worked with GM before, so I don't know if it shows up in the console log. But even if not, just do what I did: Take your little code snippet and put it in a service like jsfiddle or writecodeonline.com.

I'd just assume that there's gonna be some sort of logging even with GM, though.
Airblader is offline   Reply With Quote
Reply

Bookmarks

Tags
branch-fail, executing, greasemonkey

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:44 AM.


Advertisement
Log in to turn off these ads.