trim the last 4 characters

Discussion in 'JavaScript' started by jscripter, Dec 30, 2009.

  1. #1
    I have the following script.

    var str = "cust.txt";
    str.substring(0, str.length - 4);
    alert(str);

    I keep getting an error that says "object expected" on line 3 which is the alert.

    how do i get this to work, i want to see the vbalue of str to ensure that i have trimmed the last 4 characters.
     
    jscripter, Dec 30, 2009 IP
  2. ajith107

    ajith107 Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hi jscripter,
    You have made the original string 'str'.But here string are immutable .ie we cant make changes to original string, instead you store the value to another string say 'str1'.Then you get it right what you want.:)


    var str = "cust.txt";/* original string 'str'*/
    str1=str.substring(0, str.length - 4);/*modified string */
    alert(str1);/* i suppose you get 'cust' */
     
    ajith107, Dec 31, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    or even just:
    str = str.substring(...etc...
     
    camjohnson95, Jan 3, 2010 IP
  4. eyenan

    eyenan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi jscripter,

    U try this,
    var str = "cust.txt";
    var str1=str.split(".")[0];
    alert(str1);;)
     
    eyenan, Jan 3, 2010 IP
  5. jscripter

    jscripter Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have tried eyenan's method and ajith107's method and I still get the same error, "Object expected" line 3.

    Have u guys tried this on your own workstations to see if it works? I don't understand why I still get this error.
     
    jscripter, Jan 5, 2010 IP
  6. eyenan

    eyenan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Which editor u r using..?otherwise u simply put some alert msg, whether its alert or ot.....U chk and confirm.
    eg: alert("JScripter");
     
    eyenan, Jan 5, 2010 IP
  7. jscripter

    jscripter Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok I have tried that as well and i still get the same error. Below is the code. This code is in a file called Test.js

    It resides on my c:

    I then double click it to test it.

    var str = "cust.txt";
    var str1=str.split(".")[0];
    alert("JScripter");
     
    jscripter, Jan 8, 2010 IP
  8. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    you need to place the script in a HTML page.
    put:
    
    <html><head><title>Test</title></head><body>
    <script type="text/javascript">
    ..YOUR CODE HERE...
    </script></body></html>
    
    Code (markup):
    and save as .html.
     
    camjohnson95, Jan 9, 2010 IP
  9. Gungz

    Gungz Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Do u open it with IE 8?
     
    Gungz, Jan 11, 2010 IP
  10. jscripter

    jscripter Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    camjohnson95,

    Placing it in a htm file worked.

    Why does it work in htm file and with an extension of js?
     
    jscripter, Jan 13, 2010 IP
  11. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #11
    Because the js file needs to be attached to a html document that runs within a browser... if you just run a plain js file it will run under windows (windows script host)... which won't support a lot of of objects that a browser will, but uses the same languages (vbscript or jscript). I guess you can say that it runs like, and has similar uses as, a batch (.bat) file but for windows with a lot more possibilities. Well that is my understanding of it anyway...
     
    camjohnson95, Jan 13, 2010 IP