Page 1 of 1
Javscript full url constructions
Posted: Mon Mar 21, 2005 5:30 am
by Chris Corbyn
OK,
I think I've asked this question before and the answer was "it doesn't exist - write your own". But just to verfiy before I do write this.
I need to display a URL in the status bar. The URL must be the full
http://website.com/file.php?mode=x.
Reason: I have onClick events on some page elements (<div>s etc) and style="cursor:pointer". These look like links when mouseover'd but the status bar doesn't show the URL that will be reached by clicking it. This is what I want to do with JS.
So on a page
http://www.mysite.com/subdir1/...
Code: Select all
<div style="e;width:300px; height:300px; background:blue; cursor:pointer"e; onClick="e;window.location('./somedir/')"e;>Click here</div>
//Should show "e;http://www.mysite.com/subdir1/somedir/"e; in the status bar
and
Code: Select all
<div style="e;width:300px; height:300px; background:blue; cursor:pointer"e; onClick="e;window.location('?mode=test&x=6')"e;>Click here</div>
//Should display "e;http://www.mysite.com/subdir1/?mode=test&x=6"e; in the status
and
Code: Select all
<div style="e;width:300px; height:300px; background:blue; cursor:pointer"e; onClick="e;window.location('/newdir/')"e;>Click here</div>
//Should display "e;http://www.mysite.com/newdir/"e; in the status
Essentially, whatver <a href="newlocation">Link here</a> would show in the status bar should be identical to what the JS will show based upon a location given to it.
If I have to write my own I'll put it in snippets, or maybe somebody has done it already or theres one built in?
Cheers for any help

Posted: Mon Mar 21, 2005 6:34 am
by n00b Saibot
Quick-Thinking+Quick-Coding+Quick-Testing gives me the following code
Code: Select all
function writeURL(myURL)
{
basePath = document.location.href.substring(0,document.location.href.lastIndexOf("e;/"e;))
return basePath+"e;/"e;+myURL}
This snippet does what you want but doesn't resolve relational paths yet.
I will include it sometime if I get enough time. if you plan to do so, by all means go on.

Posted: Mon Mar 21, 2005 7:18 am
by Chris Corbyn
n00b Saibot wrote:Quick-Thinking+Quick-Coding+Quick-Testing gives me the following code
Code: Select all
function writeURL(myURL)
{
basePath = document.location.href.substring(0,document.location.href.lastIndexOf("e;/"e;))
return basePath+"e;/"e;+myURL}
This snippet does what you want but doesn't resolve relational paths yet.
I will include it sometime if I get enough time. if you plan to do so, by all means go on.

I know you're the JS daddy

But it doesn't work too well. It wont add queries to the url etc... I believe it's not so straightforward. It needs to be able to remove queries an swap them, add #name, go to root, go to relative. It's gonna need some regexp work to be efficient me thinks
I was gonna write one something like:
If 'a' = TRUE then URL is taken to be absolute. Example:
var newURL = showURL('./newdir/test.php?foo=bar', false);
Code: Select all
function showURL(loc, a) {
// Some regexp stuff and decide how to construct
//Show the URL in the status bar
}
Thanks for the reply

(Feel free to input some more LOL)
Posted: Mon Mar 21, 2005 7:29 am
by feyd
use the regexp objects then

Posted: Mon Mar 21, 2005 7:37 am
by Chris Corbyn
I'm going to. I know what to do, it's just gonna be tricky (I think). My main question was "is there a built in function?" but since there isn't and I can't see one on hotscripts (which I never use anyway) I'll do my own.
Should be done later today - check snippets later

Posted: Mon Mar 21, 2005 7:49 am
by n00b Saibot
d11wtq wrote:I was gonna write one something like:
If 'a' = TRUE then URL is taken to be absolute. Example:
var newURL = showURL('./newdir/test.php?foo=bar', false);
By GOD, we both seem to think so much on the common lines.
Do you have any regexps for this as i would like to know the depth of a relative path and
i hate regexps grrr. they always seem to go opposite to what i want them to do.

Posted: Mon Mar 21, 2005 8:14 am
by Chris Corbyn
I don't have any yet - well that's a lie actually... I have it so far to go to a subfolder of the root dir - this shouldn't actually take too long I don't think although I'm at work so I keep getting jobs to do (dammit

):
Code: Select all
function showURL(loc) {
curLoc = unescape(window.location)
if (!/^\w+:\/\//.test(loc)) {
if (/^\//.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\/]+)/)
statusURL = curLocArrayї1]+loc
}
}
window.status = statusURL
return true
}
Oh... and you may have noticed that I scrapped the boolean value since the regexps can work this out more reliably anyway
EDIT: Nearly done.... (the next bit is the trickiest part going deeper into dirs)
Code: Select all
function showURL(loc) {
statusURL = ''
curLoc = unescape(window.location)
if (!/^\w+:\/\//.test(loc)) { //Has protocol in string
if (/^\//.test(loc)) { // Starts with /
curLocArray = curLoc.match(/^(\w+:\/\/ї^\/]+)/) //All up to and excluding first / after http://
statusURL = curLocArrayї1]+loc
} else if (/^\?/.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\?]+)/)
statusURL = curLocArrayї1]+loc
} else if (/^\#/.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\#]+)/)
statusURL = curLocArrayї1]+loc
}
} else {
statusURL = loc
}
window.status = statusURL
return true
}
Posted: Mon Mar 21, 2005 10:34 am
by Chris Corbyn
Done, tested and works flawlessly:
Code: Select all
function showURL(loc) {
statusURL = ''
curLoc = unescape(window.location)
if (!/^\w+:\/\//.test(loc)) { //Has protocol in string
if (/^\//.test(loc)) { // Starts with /
curLocArray = curLoc.match(/^(\w+:\/\/ї^\/]+)/) //Up to first / after http://
statusURL = curLocArrayї1]+loc
} else if (/^\?/.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\?]+)/) //Is argument
statusURL = curLocArrayї1]+loc
} else if (/^\#/.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\#]+)/) //Is anchor name
statusURL = curLocArrayї1]+loc
} else if (locArray = loc.match(/^(\.\/)?(.+)?$/)) { //Strip the ./ if exists
subLoc = locArrayї2]
curLocArray = curLoc.match(/^(\w+:\/\/.+)\//) //Up to final /
statusURL = curLocArrayї1]+'/'+subLoc
}
} else {
statusURL = loc //Full URL (Absolute)
}
window.status = statusURL
return true
}