PDA

View Full Version : Redirecting based on country code (if, else)



sehaitt
Sep 11th, 2007, 04:22 PM
I work in a web development field. I mainly work with home brewed programs, my area is not coding. Regardless, I've been asked to do some coding work with JavaScript. I'm a complete 'noob' at this so go easy on me..

1. The company I work for is a book company and on their site, they have a search bar. When you enter (in my case) abebooks.com into the search bar, all of abebooks.com's products show up.

2. There are two sides to this website. A U.S. side and a Canadian side.

3. We no longer deliver products from abebooks.com to Canada.

4. So here is what I'm looking for. I need some sort of code so that when you enter abebooks.com in the search bar from the U.S. side, it takes you to the area on the site it always took you to (abebooks.com reseller page), but if you enter abebooks.com in the search bar from the Canadian side, it takes you to an error or 'We're Sorry' page.

Ok, so here's what I've done so far:

The 'Catch Phrases' are done on one of these home brewed programs I was talking about, which I have figured out. What my idea is, is to change the link in this catch phrase program so that it points to a seperate .asp file. (ex. /site_includes/redirectAbe.asp) and then in that .asp file set up the JavaScript to point to the two pages (U.S. 'right' page, Canadian error page).

I. /shop/toc/toc.aspx?nodeid=cb005ec48a0d448ebc8e5df855de2e0e&toc=abebooks (this is where the U.S. side should redirect you)
II. /site_includes/sorry.asp (the error apge, where Canadians get redirected)

Now, I've been looking over some JavaScript tutorials and all that but end up more confused than ever. I found some similar code on other pages in our website and tried modifying it to fit my needs with no avail. Here's my awful first attempt:


<script language="JavaScript" type="text/JavaScript">
if (readCookie("CountryCodeCookie") == "2")
{document.open("/site_includes/sorry.asp")}
else {document.open("/shop/toc/toc.aspx?nodeid=cb005ec48a0d448ebc8e5df855de2e0e&toc=abebooks")}
</script>


CountryCode: U.S.=1 Canada=2

Fang
Sep 11th, 2007, 05:08 PM
<script type="text/javascript">
if (readCookie("CountryCodeCookie") == "2")
{location.href="/site_includes/sorry.asp";}
else {location.href="/shop/toc/toc.aspx?nodeid=cb005ec48a0d448ebc8e5df855de2e0e&toc=abebooks";}
</script>

sehaitt
Sep 11th, 2007, 08:25 PM
Thank you Fang, but it still doesn't seem to work. It just gives me the blank .asp page I created and doesn't want to redirect.

Fang
Sep 11th, 2007, 09:59 PM
Are the file paths and file names correct?

sehaitt
Sep 11th, 2007, 10:11 PM
Yes, I even double checked by copying the paths straight from the code in Dreamweaver and pasting them in the address bar of my browser. They show up fine when I do that, but still not redirecting if I go to the redirectabe.asp I created. hmm...

Fang
Sep 11th, 2007, 11:06 PM
Which of the 2 statements is reached to open a blank page?

sehaitt
Sep 13th, 2007, 02:14 PM
the sorry.asp is the 'blank' page. It wouldn't be entirely blank, it would have a 'sorry, this page cannot be accessed..' message.

Fang
Sep 13th, 2007, 02:38 PM
The browser is not finding the page, the paths are probably wrong.
Try putting the full URL, including the domain, in the href string.

sehaitt
Sep 18th, 2007, 06:43 PM
Ok, I figured out the problem. It wasn't reading the Country Cookie because we were entering the redirect page from the search bar. Anyway, I reworked the code some more and its operating fine. If anyone is interested here it is:


<script language="javascript" script type="text/javascript">
var url=window.location.href;
var nohttp=url.split('//')[1];
var hostPort=nohttp.split('/')[0];
addressVAR=hostPort
if(addressVAR=="www.***edited***.com")
{location.href="/shop/toc/toc.aspx?nodeid=cb005ec48a0d448ebc8e5df855de2e0e&toc=abebooks";}
else {location.href="/site_pages/sorry.asp";}
</script>


Thanks again for all your help, Fang.