2013-07-30 80 views
1

我需要获得当前标题和5秒后重定向到:获取网页标题和重定向

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

这里是我的代码

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); 

我觉得是这样的:

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

从哪里和什么时候获得'标题页'? –

回答

1

矿比使用对象表示法更多的“硬编码”。

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

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

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

尝试使用setTimeout等待5秒钟,然后window.location设置新位置。 encodeURIComponent用于“净化”或编码URI参数(在本例中为标题)。

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

可能值得'encodeURIComponent(document.title)'。 –

+0

@DavidThomas对,我刚刚更新了我的答案。 – federicot

3

让我们重构了一下....

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

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

setTimeout是所有你需要设置5秒的延迟。 document.title获得当前标题。 countryMap是包含您希望链接到的所有文档的映射的对象。

1

重定向这样

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