My Javascript Google-fu is failing me.
June 1, 2006 9:58 AM   Subscribe

How can I replace part of an URL with a variable from a pulldown form?

I can get the form to redirect to a new page pretty easily, but I'd like it to write the current domain, replace a directory, and then append the rest of the current URL.

For example
Replace this:
http://domain.com/english/example.htm
With this:
http://domain.com/espagnol/example.htm

My Javascript skills amount to massaging already existing scripts and I haven't found any explanation to point me to know what to look for. Does anyone know of an online resource that illustrates this to help me out? Thanks!
posted by stevis to Computers & Internet (2 answers total)
 
String Replace is probably what you are looking for and if not should lead you down the right path.
posted by The Radish at 10:37 AM on June 1, 2006



You'd have a Javascript function somehwere near the top of the page:

function openURL()
{

// grab index number of the selected option
selInd = document.theForm.lang.selectedIndex;

// get the document name of this page:
theDoc = document.theForm.location.pathname;

theDomain = 'http://www.domain.com/';

// get value of the selected option
goURL = theDomain + document.theForm.lang.options[selInd].value + '/' + theDoc;


// redirect browser to the grabbed value (here a URL)
top.location.href = goURL;

}


..then where your language select list is, there's drop-down select list name="lang" and onChange="openURL()" (inside the slect tag). This select list is inside of a form name="theForm" inside the form tag.

This select list has two pull-down options:

value="english"
value="espanol"
posted by StarForce5 at 11:22 AM on June 1, 2006


« Older Nothing "deluxe" or "king" about it.   |   save my cappi Newer »
This thread is closed to new comments.