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.
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' */
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.
Which editor u r using..?otherwise u simply put some alert msg, whether its alert or ot.....U chk and confirm. eg: alert("JScripter");
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");
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, Placing it in a htm file worked. Why does it work in htm file and with an extension of js?
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...