Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    IF string contains or startswith

    Hello,

    Not sure if im in the right place, but Im a complete novice with Java.

    can someone help me with this (not so apparently) simple request.


    I need code along the lines of;

    Code:
    var str= windows.content.getelementbyID"lblMsg".text ; 
    
    if(str.search("Congratulations!") != -1) { 
    iimplay macrohere.iim
    
    else
    
    iimplay macro2.imm

    basically, I want to search the "lblmsg" for the word congratulations. If it does, I want it to run imacro1 if not, then run imacro2.


    Can anyone please help?

    ive also tried this, which works for EXACT matches, but the "lblmsg" contains the word "you've" and I dont know how to write the ' in Java :|

    Code:
    iimPlay("CODE:SET !ERRORIGNORE YES");
    for (i = 0; i <= 99998; i++) {
    if (window.content.document.getElementById("lblMsg").text == 'You've lost'){
    iimPlay("Loss.iim");
    }else 
    {
    iimPlay("win.iim");     
    }
    }

    All help is appreciated.

    Thanks in advance,

    Mike.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by robotacha View Post
    Not sure if im in the right place, but Im a complete novice with Java..
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    Code:
    if (window.content.document.getElementById("lblMsg").text == 'You\'ve lost'){

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    hi, this is JavaScript , my wording is poor apologies.
    Last edited by robotacha; 06-08-2012 at 03:49 PM.

  • #4
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your reply though

    just a quick one,

    is there an operator for contains or startswith?

    I know

    == is equal to
    != is not equal too


    could I amend that line so it says
    Code:
    .text contains 'you'
    or
    Code:
    .text.startswith 'you'
    ?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    You need to use a regular expression:-


    Code:
    <script type="text/javascript">
    
    var text = "you are a big fat guy";
    
    if (/\byou\b/gi.test (text)) {     /// match you as a whole word in the string case insenstive
    alert ("text contains the word 'you' ");
    }
    
    if (/^\byou\b/gi.test (text)) {     /// match you as a whole word at the start of the string case insensitive
    alert ("text starts with the word 'you' ");
    }
    
    else {
    alert ("Not found");
    }
    
    </script>
    Note that "you" is matched as a whole word - it will not find "your" or "you've". If you want to match you not as a whole word remove the \b metacharcters.
    Last edited by Philip M; 06-08-2012 at 04:06 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    robotacha (06-08-2012)

  • #6
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hi, thanks for that.

    as the string im searching is:


    Code:
    windows.content.getelementbyID"lblMsg".text
    do i need to wrap quotes around this?

    so it reads:

    Code:
    var text = "windows.content.getelementbyID"lblMsg".text" ;
    or leave it as:

    Code:
    var text =  windows.content.getelementbyID"lblMsg".text ;
    ?


    so do you think this code would work?

    Code:
    <script type="text/javascript">
    
    var text =  "windows.content.getelementbyID"lblMsg".text" ;
    
    if (/^\byou\b/gi.test (text)) {     
    iimplay ("loss.imm");
    }
    
    else {
    iimplay ("win.imm");
    }
    
    </script>

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    You seem to be struggling with Javascript syntax. Note that Javascript is case-sensitive.

    Code:
    var text = getElementById("lblMsg").value;  // if lblMsg is a form field
    
    var text = getElementById("lblMsg").innerHTML; //  if lblMsg is a DOM element
    I have no idea what iimplay is supposed to be.

    It is a poor idea to use the letter l in your element id's as it is easily confused with the number 1.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello,

    Again, thanks for the reply.

    Yes, I am struggling with the syntax.
    I use VBA and crystal syntax a hell of a lot which is alot simpler than Java. (if im honest, ive never used any javascript syntax)

    Im being really brute with this script.

    basically im using iMacros to run a script/macro.

    I have macros similar that other people have wrote for me. e.g: test.js

    these work fine and follow the same sort of format I am trying to achieve.


    iimplay will run an imacro script based on the result of the IF javascript.

    looking at that though,

    certainly

    Code:
    var text = getElementById("lblMsg").value;  // if lblMsg is a form field
    seems to be the one I am looking for.


    I will report back and let you know

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by robotacha View Post
    Yes, I am struggling with the syntax.
    I use VBA and crystal syntax a hell of a lot which is alot simpler than Java. (if im honest, ive never used any javascript syntax)
    If you cannot understand that Java !=Javascript then I would have thought that your difficulties would multiply. There is no sense in using VBA or Java syntax, anymore than trying to use English words or word order in German.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •