jose sanchez
jose sanchez

Reputation: 339

Get the title page and redirect

I need to get the current title and redirect after 5 seconds to:

http://mysite.org/redirect1.php?title=TITLE PAGE WHIT JAVASCRIPT

Here is my code

if(country=="MX"){
    url="http://mysite.org/redirect1.php?title=TITLE PAGE";
} else if (country == "ES") {
    url="http://mysite.org/redirect2.php?title=TITLE PAGE";
} else if (country == "PE") {
    url="http://mysite.org/redirect1.php?title=TITLE PAGE";
} else if (country == "AR") {
    url="http://mysite.org/redirect3.php?title=TITLE PAGE";
} else if (country == "PY") {
    url="http://mysite.org/redirect4.php?title=TITLE PAGE";
} else if (country == "CO") {
    url="http://mysite.org/redirect1.php?title=TITLE PAGE";
} else if (country == "CL") {
    url="http://mysite.org/redirect1.php?title=TITLE PAGE";
} else {
    url="http://mysite.org/blank.htm";
}
setTimeout("location.href = url;",5000);

I think something like:

var title = document.title;
    if(country=="MX"){
        url="http://mysite.org/redirect1.php?title"+title;
    }

Upvotes: 1

Views: 317

Answers (4)

Jacob Morrison
Jacob Morrison

Reputation: 648

Mine is a bit more "hardcoded" than using the object notation.

var url = "http://mysite.org/@REDIRECT@";
var blank;
var timeout = 5000;
switch (country) {
    case "MX":
        url = url.replace("@REDIRECT@", "redirect1.php");
        break;
    case "ES":
        url = url.replace("@REDIRECT@", "redirect2.php");
        break;
    case "PE":
        url = url.replace("@REDIRECT@", "redirect1.php");
        break;
    case "AR": 
        url = url.replace("@REDIRECT@", "redirect3.php");
        break;
    case "PY":
        url = url.replace("@REDIRECT@", "redirect4.php");
        break;
    case "CO":
        url = url.replace("@REDIRECT@", "redirect1.php");
        break;
    case "CL":
        url = url.replace("@REDIRECT@", "redirect1.php");
        break;  
    default:
        url = url.replace("@REDIRECT@", "blank.htm");
        blank = true;
}

if (!blank) {
    url += "?title=" + encodeURIComponent(document.title);
}

setTimeout(function() {
    window.location.href = url;
}, timeout);

Upvotes: 1

federicot
federicot

Reputation: 12341

Try this using setTimeout to wait 5 seconds, and window.location to set the new location. encodeURIComponent is used to "sanitize" or encode a URI parameter (in this case, the title).

var delay = 5000; // 5 seconds in milliseconds
setTimeout(function() {
    window.location = 'http://mysite.org/redirect1.php?title=' + encodeURIComponent(document.title); 
}, delay);

Upvotes: 1

Max Leps
Max Leps

Reputation: 479

redirect like this

window.setTimeout(function() {
   window.location = "http://mysite.org/redirect1.php?title"+title;
}, 5000);

Upvotes: 1

Brad
Brad

Reputation: 163262

Let's refactor a bit....

var countryMap = {
  MX: 'redirect1.php',
  PE: 'redirect2.php',
  /* etc. */
};

setTimeout(function () {
    window.location = 'http://mysite.org/' + countryMap[country] + '?title=' + encodeURIComponent(document.title);
}, 5000);

setTimeout is all you need to set that 5-second delay. document.title get the current title. The countryMap is an object containing a map to all the documents that you wish to link to.

Upvotes: 3

Related Questions