RE: using a variable in variable.lastIndexOf not working properly
by Chuck Evans <Chucke(at)captura.com>
|
| Date: |
Wed, 12 Jan 2000 19:30:20 -0800 |
| To: |
"'HWG Languages'" <hwg-languages(at)hwg.org> |
| Cc: |
Chuck Evans <Chucke(at)captura.com> |
| |
todo: View
Thread,
Original
|
|
I love when I can dig and dig and dig and answer my own question before it
posts on the list. So here's my "Javascript hacker" solution to my own
problem. The short answer to my original question was to officially declare
the variable as a string, since the URL returned wasn't clearly a string and
Javascript didn't know what to do with it:
var loc_string = document.location;
s = new String(loc_string);
However, I got to digging and came up with a better way to do what I wanted
to do. Since I want to just remove the first three letters of the path (for
example, /AB) and replace with a different slash and two-letter pair, I'm
using this:
<script language="JavaScript">
<!--
var loc_URL = document.location;
// gives me the raw URL
var path_string = loc_URL.pathname;
// gives me a document path, without the http and domain
var length_of_string = path_string.length;
// returns the length of the path string
var path_substring = path_string.substring(3,length_of_string);
// returns a new string, removing the first three characters from the first
string
//-->
</script>
Now I can just use String.link to create some new links. What this will do
is allow me to put a link on a page in one directory structure that will
link to the same page in a different directory structure. For example,
/ab/directory/page.html can link to /cd/directory/page.html. I can use this
code on every page without modifying it for each page.
---------------------------------------------------------
Chuck Evans
Web Marketing Manager
Captura Software, Inc.
(425) 424-1155
chucke(at)captura.com
www.captura.com
> -----Original Message-----
> I'm trying to use javascript to return the last position of a slash in a
> URL:
>
> <script language="JavaScript">
> <!--
> var loc_string = document.location;
> var last_index = loc_string.lastIndexOf('/');
> document.write(last_index);
> //-->
> </script>
>
> Every time I load the page, though, I get the error: Object doesn't
> support this property or method
>
> If I replace loc_string with like with an actual string in quotes (second
> t in http left of purposely to avoid making a real hyperlink):
>
> var last_index = "htp://www.mypage.com/index.html".lastIndexOf('/');
>
> Then it returns an index position correctly. Any idea what I'm doing
> wrong? Shouldn't I be able to use variablename.lastIndexOf() after
> creating and defining the variable?
>
> FYI, ultimately, I want a function that will strip everything before the
> last / in a URL so I can then replace it with a different URL. For
> example, I want to be able to turn
> htp://www.mypage.com/directory/subdir/index.html into
> htp://www.mypage.com/directory/otherdir/index.html.
>
> Any guesses on how to do that would be appreciated too. Another way to do
> it would be to find /subdir/ in the string and replace it with /otherdir/
> but I can't begin to work on that part of the function until I get the
> lastIndexOf part working.
>
HWG: hwg-languages mailing list archives,
maintained by Webmasters @ IWA
This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.