BillHely
asked on
using indexof & substring
Plesae don't laugh. I'm fair at VBScript and totally clueless at Javascript. I've got to very slightly modify a page based on some Javascript that was written by someone else.
Here's the error I'm encountering:
Microsoft JScript runtime error '800a01b6'
Object doesn't support this property or method
blah blah blah at line 100
EXPLANATION:
~~~~~~~~~
There is a variable called PathStr, which is a partial path/filename. An example of a possible value of PathStr is:
progressimages\7997-80720. 13\255 KP - Aslab.JPG
I need to extract just the Filename without the extension. The code segment I've tried to use is:
// The next line is Line 100 where the error occurs.
var offset1 = PathStr.indexof("\");
var offset1 = PathStr.indexof(".jpg");
var Caption = PathStr.substring(offset1, offset2);
Using the PathStr example value given above, I believe I'd end up with "7997-80720.13\255 KP - Aslab", then I'd do it again to get down to just the filename.
But dammed if I can see what's wrong with the offset1 and offeset2 lines. Just a syntax error or am I on the wrong track completely?
Or maybe there is a better, simpler way altogether?
TIA
- Bill.
Here's the error I'm encountering:
Microsoft JScript runtime error '800a01b6'
Object doesn't support this property or method
blah blah blah at line 100
EXPLANATION:
~~~~~~~~~
There is a variable called PathStr, which is a partial path/filename. An example of a possible value of PathStr is:
progressimages\7997-80720.
I need to extract just the Filename without the extension. The code segment I've tried to use is:
// The next line is Line 100 where the error occurs.
var offset1 = PathStr.indexof("\");
var offset1 = PathStr.indexof(".jpg");
var Caption = PathStr.substring(offset1,
Using the PathStr example value given above, I believe I'd end up with "7997-80720.13\255 KP - Aslab", then I'd do it again to get down to just the filename.
But dammed if I can see what's wrong with the offset1 and offeset2 lines. Just a syntax error or am I on the wrong track completely?
Or maybe there is a better, simpler way altogether?
TIA
- Bill.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
var offset1 = PathStr.indexof("\");
it thinks there isn't another quote because a backslash escapes a quote, so you'd need to do something like:
var offset1 = PathStr.indexof("\\");
it thinks there isn't another quote because a backslash escapes a quote, so you'd need to do something like:
var offset1 = PathStr.indexof("\\");
This will get you closer.
<script language="JavaScript">
<!--
var PathStr = "progressimages\7997-80720 .13\255 KP - Aslab.JPG".toString().quot e();
alert(PathStr);
var lcPathStr = PathStr.toLowerCase();
var offset1 = lcPathStr.indexOf("\\", lcPathStr.indexOf("\\") + 1) + 1;
var offset2 = lcPathStr.indexOf(".jpg");
alert(offset1);
alert(offset2);
var Caption = PathStr.substring(offset1, offset2);
document.writeln(Caption);
-->
</script>
I'm still trying to determing why the 255 shows as "x". I think it's because of the .quote() function, which I have used here to escape the \ with another \.
<script language="JavaScript">
<!--
var PathStr = "progressimages\7997-80720
alert(PathStr);
var lcPathStr = PathStr.toLowerCase();
var offset1 = lcPathStr.indexOf("\\", lcPathStr.indexOf("\\") + 1) + 1;
var offset2 = lcPathStr.indexOf(".jpg");
alert(offset1);
alert(offset2);
var Caption = PathStr.substring(offset1,
document.writeln(Caption);
-->
</script>
I'm still trying to determing why the 255 shows as "x". I think it's because of the .quote() function, which I have used here to escape the \ with another \.
This is a great example of why you really shouldn't start file names with numbers...
Could you try this :
fileName = PathStr.substr(PathStr.las tIndexOf(' \\')+1);
.. I think this will get you what you want, However, Im in class right now andI havent tested it :(
Sorry,
Cheers hope this works.
fileName = PathStr.substr(PathStr.las
.. I think this will get you what you want, However, Im in class right now andI havent tested it :(
Sorry,
Cheers hope this works.
or you could forget the indexOf/substr junk and use a regex:
PathStr = "progressimages\\7997-8072 0.13\\255 KP - Aslab.JPG"
fileName = PathStr.match(/^.*?\\([^\\ ]*?)\.[^.] +$/)[1];
alert(fileName);
PathStr = "progressimages\\7997-8072
fileName = PathStr.match(/^.*?\\([^\\
alert(fileName);
oh.. you also need to define your var PathName with double slashes. or use the unescape to get away form the \'s.
Cheers, the following works I tested it.
<HTML>
<SCRIPT>
function doThis()
{
var PathStr = "progressimages\\7997-8072 0.13\\255 KP - Aslab.JPG";
alert(PathStr);
var fileName;
fileName = PathStr.substr(PathStr.las tIndexOf(' \\')+1);
alert( fileName );
}
</SCRIPT>
<HEAD></HEAD>
<BODY>
<input type="button" onClick="doThis();" value="doThis()">
</BODY>
</HTML>
Cheers, the following works I tested it.
<HTML>
<SCRIPT>
function doThis()
{
var PathStr = "progressimages\\7997-8072
alert(PathStr);
var fileName;
fileName = PathStr.substr(PathStr.las
alert( fileName );
}
</SCRIPT>
<HEAD></HEAD>
<BODY>
<input type="button" onClick="doThis();" value="doThis()">
</BODY>
</HTML>
ASKER
Well, everybody helped and I got some better ideas, but unfortunately I can't split the money.
So in all fairness I'll have to accept the first answer that actually resolved the error I was getting at the time.
The immediate problem was the case of "indexof" instead of 'indexOf', which StormyWaters was the first to point out.
Thanks everyone. Doubtless I'll be back :-(
- Bill
So in all fairness I'll have to accept the first answer that actually resolved the error I was getting at the time.
The immediate problem was the case of "indexof" instead of 'indexOf', which StormyWaters was the first to point out.
Thanks everyone. Doubtless I'll be back :-(
- Bill
Note: You can "split the money." There is a link to "split" at the bottom.
ASKER
> Note: You can "split the money." There is a link to "split" at the bottom.
My apologies to all. I didn't realize that. So much for making "assumptions"! Thanks for pointing it out --- I'll know for next time.
substring is actually substr, and you're defining 'offset1' twice instead of having the second as 'offset2'