View Full Version : javascript question *current URL*
shuga
June 25th, 2003, 11:20 AM
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
Dravos
June 25th, 2003, 11:30 AM
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:
myIndex=location.href.indexOf("/")
myString=location.href.substring(0,myIndex)
if you have the http:// in the result then you need to start the index further on so...
myIndex=location.href.indexOf("/",7)
myString=location.href.substring(0,myIndex)
this would skip the first 2 "/"s
shuga
June 25th, 2003, 11:33 AM
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 :)
shuga
June 25th, 2003, 11:40 AM
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
Dravos
June 25th, 2003, 11:41 AM
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
shuga
June 25th, 2003, 12:02 PM
sweet ... i'll post it to the web and try it out ... it seems to work locally :)
lostinbeta
June 25th, 2003, 12:03 PM
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"
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.
shuga
June 25th, 2003, 12:12 PM
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>
lostinbeta
June 25th, 2003, 12:20 PM
Locally it gets weird, but on the web it seems to work...
http://www.lostinbeta.com/javascript/truncateURL.html
Actually wait....
shuga
June 25th, 2003, 12:21 PM
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 :)
thanks very much
lostinbeta
June 25th, 2003, 12:24 PM
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.
lostinbeta
June 25th, 2003, 12:27 PM
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
shuga
June 25th, 2003, 12:35 PM
sweet .... it works .... http://www.aaronsleeper.com/blockpage/blockpage.html
thanks =)
lostinbeta
June 25th, 2003, 12:36 PM
I still get http at the beginning, is that intentional?
Dravos
June 25th, 2003, 02:20 PM
:cyborg: Yeah use lostinbetas updated code if you wanna get rid of http://.
Thanks lost :ninja: :evil:
Adios
lostinbeta
June 25th, 2003, 02:23 PM
No problem, you provided the original script, I just changed a 0 to a 7 to update it :P
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.