using indexof & substring

Posted on 2004-09-14
Last Modified: 2010-05-18
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


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?

 - Bill.
Question by:BillHely
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 13

Expert Comment

ID: 12060478
Two things:

substring is actually substr, and you're defining 'offset1' twice instead of having the second as 'offset2'
LVL 13

Accepted Solution

StormyWaters earned 50 total points
ID: 12060480
Oh, and indexof should be 'indexOf', javascript is case sensitive. 'substr' remains the same, I believe.

Expert Comment

ID: 12060484
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("\\");
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Expert Comment

ID: 12060584
This will get you closer.

<script language="JavaScript">
   var PathStr = "progressimages\7997-80720.13\255 KP - Aslab.JPG".toString().quote();
   var lcPathStr = PathStr.toLowerCase();
   var offset1 = lcPathStr.indexOf("\\", lcPathStr.indexOf("\\") + 1) + 1;
   var offset2 = lcPathStr.indexOf(".jpg");
   var Caption = PathStr.substring(offset1, offset2);

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 \.
LVL 13

Expert Comment

ID: 12060597

This is a great example of why you really shouldn't start file names with numbers...

Expert Comment

ID: 12060635
Could you try this :

fileName = PathStr.substr(PathStr.lastIndexOf('\\')+1);

.. I think this will get you what you want, However, Im in class right now andI havent tested it :(

Cheers hope this works.

Expert Comment

ID: 12060655
or you could forget the indexOf/substr junk and use a regex:

  PathStr = "progressimages\\7997-80720.13\\255 KP - Aslab.JPG"
  fileName = PathStr.match(/^.*?\\([^\\]*?)\.[^.]+$/)[1];

Expert Comment

ID: 12060662
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.

function doThis()
     var PathStr = "progressimages\\7997-80720.13\\255 KP - Aslab.JPG";
     var fileName;
          fileName = PathStr.substr(PathStr.lastIndexOf('\\')+1);
     alert( fileName );
<input type="button" onClick="doThis();" value="doThis()">

Author Comment

ID: 12061113
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
LVL 13

Expert Comment

ID: 12065181
Note: You can "split the money."  There is a link to "split" at the bottom.

Author Comment

ID: 12070002

> 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.

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arrays and buttons with user input 2 42
make control visible based off other control value 7 45
Hta File displays dynamic File names 4 65
Jquery syntax 12 30
In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question