使用 Javascript 根据 URL 参数重定向到特定网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11823784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirection to a specific web page based on URL parameter using Javascript
提问by this-Me
I have a simple .htm web page kept in different folders for handling different languages.
我有一个简单的 .htm 网页,保存在不同的文件夹中,用于处理不同的语言。
default.htm inside en folder (en\default.htmand de\default.htmand so on)
en 文件夹中的 default.htm(en\default.htm和de\default.htm等)
I need to redirect to a specific web page based on the URL parameter i.e. if the user had
我需要根据 URL 参数重定向到特定的网页,即如果用户有
specified http://localhost/website/default.htm?lang=de
, i need to redirect him to the
指定http://localhost/website/default.htm?lang=de
,我需要将他重定向到
de\default.htmfile. i.e. a german web page.
de\default.htm文件。即一个德国网页。
had it been the ASPX pages i would have done away with the job easily with ResourceManager
如果是 ASPX 页面,我会使用 ResourceManager 轻松完成这项工作
and an appropriate .resx file using the Request.QueryStringoption provided by .NET
以及使用.NET 提供的Request.QueryString选项的适当 .resx 文件
BCL. However since i'm using plain HTML page i do not have an expertise to write a client
BCL。但是,由于我使用的是纯 HTML 页面,因此我没有编写客户端的专业知识
side script like javascript to query for the URL parameters and redirect the user to the
像javascript这样的侧脚本来查询URL参数并将用户重定向到
required page.
需要的页面。
Question :
问题 :
Can anyone guide me how do i achieve the same using any form of client scripting to
谁能指导我如何使用任何形式的客户端脚本来实现相同的目标
achieve the redirection ?? And where do i invoke the script function ?
实现重定向??我在哪里调用脚本函数?
i.e query the parameter for each post event.??
即查询每个帖子事件的参数。??
Thanks a ton
万分感谢
回答by Feisty Mango
You can use javascript to get a list of params pretty easily with the following line.
您可以使用 javascript 通过以下行轻松获取参数列表。
var paramArray = window.location.search.substring(1).split("&")
var paramArray = window.location.search.substring(1).split("&")
This will build an array of the parameters of the query string. From there you just need to add logic to find the param you specified in your question and take the appropriate redirect using
这将构建查询字符串的参数数组。从那里你只需要添加逻辑来找到你在问题中指定的参数并使用适当的重定向
window.location.href = 'some URL'; //causes the browser to refresh with the new URL
window.location.href = 'some URL'; //causes the browser to refresh with the new URL
Example:
例子:
function getQueryStringArray(){
var assoc=[];
var items = window.location.search.substring(1).split('&');
for(var j = 0; j < items.length; j++) {
var a = items[j].split('='); assoc[a[0]] = a[1];
}
return assoc;
}
//point at which you want to determine redirection
var qs = getQueryStringArray();
var url = '';
if (qs.lang !== 'undefined' && qs.lang) {
switch (qs.lang) {
case 'en':
url = 'blah';
break;
case 'de':
url = 'meh';
break;
}
window.location.href = url; //reroute
}
回答by this-Me
See this on how to parse query string parameters using jQuery How can I get query string values in JavaScript?
请参阅有关如何使用 jQuery 解析查询字符串参数如何在 JavaScript 中获取查询字符串值的信息?
Then you can redirect to another page with window.location
然后你可以使用 window.location 重定向到另一个页面
Something like this
像这样的东西
<script>
$(document).ready(function(){
var p = getParameterByName("lang");
var rootUrl = "yourRootUrl";
var url = rootUrl + p + '/default.htm';
window.location = url;
});
</script>
No jQuery
没有 jQuery
<script>
(function(){
var p = getParameterByName("lang");
var rootUrl = "yourRootUrl/";
var url = rootUrl + p + '/default.htm';
window.location = url;
}());
</script>
回答by SReject
The following should do it for you. Just wrap it in <script></script>
tags.
以下应该为你做。只需将其包裹在<script></script>
标签中即可。
// Locate "lang=...." in the url using regex;
var a = /[\?&]lang=([^\/&#\?]+)/i.exec(window.location.pathname);
// check if the regex matched
if (a) {
// If so, redirect the user
window.location.href = "http://localhost/website/" + a[1] + "/index.htm";
}
This will save you from having to loop through the url, but it does require a basic understanding of how regexp works.
这将使您不必遍历 url,但它确实需要对 regexp 的工作原理有基本的了解。