Javascript question *current URL*

i have this code
in javascript


   
           <!--
           {
             document.write(location.href);
           }
           // -->

and I have a page that needs to display the url that is currently in the address bar. it’s hard to understand why i need this w/o knowing my company’s product but i unfortunately can’t explain here.

anyway so I would like to take this script and make it truncate the resulting string so that if the location.href resolves to www.yahoo.com/soup/reallylongname/monkey then the page will display www.yahoo.com

any help is appreciated

thanks

So you want to truncate to the first “/”.

so u use indexOf() to find the “/” and use that as your end point then use substring to truncate

so:
[AS]
myIndex=location.href.indexOf("/")
myString=location.href.substring(0,myIndex)
[/AS]

if you have the http:// in the result then you need to start the index further on so…

[AS]
myIndex=location.href.indexOf("/",7)
myString=location.href.substring(0,myIndex)
[/AS]

this would skip the first 2 "/"s

so my final script should read


<!--
           {
document.write(location.href);
myIndex=location.href.indexOf("/",7)
myString=location.href.substring(0,myIndex)
           }
           // -->

??

thanks very much for the help and the patience … i’m a newb scripter :slight_smile:

when i put that code into an html file and then view it … it returns the whole string "file:///C:/Documents%20and%20Settings/aaron/Desktop/Untitled-1.htm "

which i know is different if i were on the web b/c of the number of /'s but it doesn’t seem to be truncating at all

Sorry i forgot to add ; to end of my lines and the write line gets done after its truncated and uses the myString variable instead.

So try this:


<!--
           {

myIndex=location.href.indexOf("/",7);
myString=location.href.substring(0,myIndex);
document.write(myString);
           }
           // -->

Hopefully it works, else im going home now, so not back till tomorrow…

Cya

sweet … i’ll post it to the web and try it out … it seems to work locally :slight_smile:

Just for alternatives…

This will return the full URL (http and all)

document.write(window.location);

Example Output: http://www.lostinbeta.com/

And this is just for the url… (will remove http and any “/” at the very end of the line)

var myUrl = String(window.location);
document.write(myUrl.slice(7, -1));

Example Output: if the url were “http://www.lostinbeta.com/” , this would reduce it to “www.lostinbeta.com

[edit]
yes of course if there is no"/" at the end of the line the “m” in .com (or whatever the last letter is) will get cut off, but if I remember correctly browsers automatically end URLs with “/” and add “http://” to the beginning, so I don’t think this will be a problem.
[/edit]

there’s my html … the code worked fine when it was the only thing on the page but when i tried to insert it into the text … it doesn’t work … it still returns the url … but it doesn’t do the truncation


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Anonymizer Block Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="A30101">
<table width="400" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> <p align="center"><img src="AnonLogo-WHITE.gif" width="400" height="78"><br>
<b><font color="#FFFFFF" size="4" face="Arial, Helvetica, sans-serif"><br>
You have chosen to block <SCRIPT LANGUAGE="JavaScript">

<!--
{

var myUrl = String(window.location);
document.write(myUrl.slice(7, -1));
}
// -->
</SCRIPT>.</font><font color="#FFFFFF" size="5" face="Arial, Helvetica, sans-serif"><br>
<font size="4"><br>
</font> </font><font color="#FFFFFF" size="4" face="Arial, Helvetica, sans-serif">If 
you wish to view it, </font></b><b><font color="#FFFFFF" size="4" face="Arial, Helvetica, sans-serif">please 
select a different privacy level for this site.</font></b></p>
</td>
</tr>
</table>
</body>
</html>

Locally it gets weird, but on the web it seems to work…

http://www.lostinbeta.com/javascript/truncateURL.html

Actually wait…

hmm … it does work on your site … but i need the end of that truncated as well … not just the http:// so that everything after the .com/ is gone

so the url will display as www.yahoo.com or www.lostinbeta.com in the case of the file you have :slight_smile:

thanks very much

Oh alright, I gotcha now, at first I was a bit confused as to what you were looking for. Dravos’ method seems to be your best choice.

Using Dravos’ script still adds the http to the beginning, so to fix that just make a minor edit…

var myIndex = location.href.indexOf("/",7);
var myString = location.href.substring(7,myIndex);
document.write(myString);

http://www.lostinbeta.com/javascript/truncateURL.html

sweet … it works … http://www.aaronsleeper.com/blockpage/blockpage.html

thanks =)

I still get http at the beginning, is that intentional?

:cyborg: Yeah use lostinbetas updated code if you wanna get rid of http://.

Thanks lost :ninja: :evil:

Adios

No problem, you provided the original script, I just changed a 0 to a 7 to update it :stuck_out_tongue: