Welcome to WebmasterWorld Guest from

Forum Moderators: open

Help needed on Search and replace string

1:56 am on Jan 22, 2012 (gmt 0)


I was wondering if someone could help me out with this one.

I tried my best at researching the "search and replace" string way using Javascript to do the following with no success, so I came here to ask this:

I am looking for a Javascript that will find an instance of "/e" and replace it with "/f" as well as find an instance of "_e.shtml" to "_f.shtml" from within an HREF call.

In a nutshell, what I would like is to change a linked document pointing to its French equivalent. So if someone was in www.site.com/e/welcome_e.shtml then the "this page in French" link would point to www.site.com/f/welcome_f.shtml any way of doing this, while being linkable (href)?

Thank you

1:57 pm on Jan 22, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month


var enText = "www.example.com/e/welcome_e.shtml";
var frText = enText.replace("\/e\/", "\/f\/");
frText = frText.replace("_e.shtml", "_f.shtml");
2:00 pm on Jan 22, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Note, JavaScript is probably not a good solution here. JavaScript should be optional, used for progressive enhancement, so a server side solution would probably be better.

Also, you may want to use standard values for English (en) and French (fr) instead of just e and f.
3:56 pm on Jan 22, 2012 (gmt 0)

Thanks for the reply (Fotiman). I do have a .pl script that will do that, yet with the Godaddy account that I have for a friend's account, it does not let me run a perl script unless I pay them more, which is not an option.

The Perl script that I have, automatically detects a document's name using $ENV{'HTTP_REFERER'} which then passes it to any document's name (i.e. contact_us_e.shtml or about_e.shtml from within a "/server's root/e" folder or /f, and shows the equivalent's language no matter which language they are reading.

Yes, I agree using Javascript is an 'option', and tried to find a server-side option other than the Perl script, but did not find anything on the Web, nor was I able to build one from scratch. The closest I came to was <A HREF="../en/<!--#echo var="DOCUMENT_NAME"-->">This document in English</A> but that would mean that I would have to have the same document's name (welcome.shtml in both English and French) and that leaves room for errors in so many ways (i.e. accidentally overwriting).

Since I like working on a "Global change" scale, I like using .shtml to "include" files, so if something changes, I won't have 40 files to change. The "this document in french" is presently a link as this <a href="/cgi-bin/language_change.pl">English</a> and the same href call for <a href="/cgi-bin/language_change.pl">ce document en anglais</a>. That's why I was looking for a Javascript equivalent.
8:25 pm on Jan 22, 2012 (gmt 0)

Here is my original Perl file that works on other servers with CGI capabilities, but not Godaddy. Any way to convert this to run as a Javascript or SSI (server-side include)?


$calling_page = $ENV{'HTTP_REFERER'};

if($calling_page =~ /(.*)\#.*/) {

$calling_page = $1;}

# ignore any file that is not _e.shtml or _f.shtml and do nothing!

# is this an _e.shtml file?

if($calling_page =~ /_e\.shtml/) {

# replace the suffix

$calling_page =~ s{/e/(.+)_e\.shtml}{/f/$1_f.shtml};

print "Location: $calling_page\n\n";

# then is this an _f.shtml file?


elsif($calling_page =~ /_f\.shtml/) {
# replace the suffix
$calling_page =~ s{/f/(.+)_f\.shtml}{/e/$1_e.shtml};
print "Location: $calling_page\n\n";}

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month