Javscript full url constructions

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Javscript full url constructions

Post 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=&quote;width:300px; height:300px; background:blue; cursor:pointer&quote; onClick=&quote;window.location('./somedir/')&quote;>Click here</div>

//Should show &quote;http://www.mysite.com/subdir1/somedir/&quote; in the status bar
and

Code: Select all

<div style=&quote;width:300px; height:300px; background:blue; cursor:pointer&quote; onClick=&quote;window.location('?mode=test&x=6')&quote;>Click here</div>

//Should display &quote;http://www.mysite.com/subdir1/?mode=test&x=6&quote; in the status
and

Code: Select all

<div style=&quote;width:300px; height:300px; background:blue; cursor:pointer&quote; onClick=&quote;window.location('/newdir/')&quote;>Click here</div>

//Should display &quote;http://www.mysite.com/newdir/&quote; 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 :lol:
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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(&quote;/&quote;))
return basePath+&quote;/&quote;+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. :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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(&quote;/&quote;))
return basePath+&quote;/&quote;+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. :D
I know you're the JS daddy :lol: 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 :D (Feel free to input some more LOL)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use the regexp objects then :P
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :P
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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. :lol:
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. :( :lol:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :P ):

Code: Select all

function showURL(loc) {
	curLoc = unescape(window.location)
	if (!/^\w+:\/\//.test(loc)) {
		if (/^\//.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\/]+)/)
			statusURL = curLocArray&#1111;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 :-D

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+:\/\/&#1111;^\/]+)/) //All up to and excluding first / after http://
			statusURL = curLocArray&#1111;1]+loc
		} else if (/^\?/.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\?]+)/)
			statusURL = curLocArray&#1111;1]+loc
		} else if (/^\#/.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\#]+)/)
			statusURL = curLocArray&#1111;1]+loc
		}
	} else {
		statusURL = loc
	}
	window.status = statusURL
	return true
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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+:\/\/&#1111;^\/]+)/) //Up to first / after http://
			statusURL = curLocArray&#1111;1]+loc
		} else if (/^\?/.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\?]+)/) //Is argument
			statusURL = curLocArray&#1111;1]+loc
		} else if (/^\#/.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\#]+)/) //Is anchor name
			statusURL = curLocArray&#1111;1]+loc
		} else if (locArray = loc.match(/^(\.\/)?(.+)?$/)) { //Strip the ./ if exists
			subLoc = locArray&#1111;2]
			curLocArray = curLoc.match(/^(\w+:\/\/.+)\//) //Up to final /
			statusURL = curLocArray&#1111;1]+'/'+subLoc
			
		}
	} else {
		statusURL = loc //Full URL (Absolute)
	}
	window.status = statusURL
	return true
}
Post Reply