如何使用 JavaScript 进行页面重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2201522/
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
How to make a page redirect using JavaScript?
提问by neko
I'm making a shopping mall website using a Yahoo solution. I have multiple options for items and some items have different price dependent on the options.
我正在使用雅虎解决方案制作购物中心网站。我有多种物品选择,有些物品的价格取决于选项。
Yahoo doesn't support multiple price options, and therefore I try to find a solution for this problem. One of my ideas is to make multiple pages and redirect the page dependent on options. For example, if a customer chose model A, the page will stay in the page A which displays $1000. If a customer chose model B, the page will redirect to the page B which displays $500.
雅虎不支持多种价格选项,因此我尝试为这个问题找到解决方案。我的想法之一是制作多个页面并根据选项重定向页面。例如,如果客户选择型号 A,则页面将停留在显示 $1000 的页面 A。如果客户选择模型 B,页面将重定向到显示 $500 的页面 B。
I have already made dynamic options with JavaScript, but I want to modify it to redirect a page. Here is the link of my page:
我已经用 JavaScript 做了动态选项,但我想修改它以重定向页面。这是我的页面的链接:
http://parseven.com/callaway_diabloedge_iron.html
http://parseven.com/callaway_diabloedge_iron.html
In the page, there are options in the middle. If a customer chose his/her hand, it shows options, "#4 Thru AW," "Lob Wedge," and "Sand Wedge." If a customer chose either "Lob Wedge" or "Sand Wedge', the page has to redirect to a page which has a different price.
在页面中,中间有选项。如果客户选择了他/她的手,它会显示选项“#4 Thru AW”、“Lob Wedge”和“Sand Wedge”。如果客户选择“Lob Wedge”或“Sand Wedge”,则页面必须重定向到具有不同价格的页面。
PS:
PS:
I'm using JavaScript to generate options dependent on the previous option. The code is:
我正在使用 JavaScript 生成依赖于前一个选项的选项。代码是:
<script type="text/javascript" language="javascript">
<!--
document.write('<select name="Iron(s)" onChange="javascript: listboxchange (this.options[this.selectedIndex].value);"><option value="">Select Iron(s)</option></select>')
-->
</script>
采纳答案by Vivin Paliath
Use:
用:
window.location = "http://my.url.here";
Here's some quick-n-dirty code that uses jQuery to do what you want. I highly recommend using jQuery. It'll make things a lot more easier for you, especially since you're new to JavaScript.
这是一些使用 jQuery 执行您想要的操作的快速代码。我强烈推荐使用 jQuery。它会让你的事情变得更容易,特别是因为你是 JavaScript 的新手。
<select id = "pricingOptions" name = "pricingOptions">
<option value = "500">Option A</option>
<option value = "1000">Option B</option>
</select>
<script type = "text/javascript" language = "javascript">
jQuery(document).ready(function() {
jQuery("#pricingOptions").change(function() {
if(this.options[this.selectedIndex].value == "500") {
window.location = "http://example.com/foo.php?option=500";
}
});
});
</script>
回答by Sarfraz
You can append the values in the query string for the next page to see and process. You can wrap them inside the link tags:
您可以在查询字符串中附加值以供下一页查看和处理。您可以将它们包装在链接标签中:
<a href="your_page.php?var1=value1&var2=value2">
You separate each of those values with the &sign.
您用&符号分隔这些值中的每一个。
Or you can create this on a button click like this:
或者,您可以像这样单击按钮来创建它:
<input type="button" onclick="document.location.href = 'your_page.php?var1=value1&var2=value2';">
回答by Rohan
Use:
用:
document.location.href = "http://yoursite.com" + document.getElementById('somefield');
That would get the value of some text field or hidden field, and add it to your site URL to get a new URL (href). You can modify this to suit your needs.
这将获取某些文本字段或隐藏字段的值,并将其添加到您的站点 URL 以获取新 URL (href)。您可以修改它以满足您的需要。
回答by Yuriy Zanichkovskyy
You can achieve this using the locationobject.
您可以使用location对象来实现这一点。
location.href = "http://someurl";
回答by Ravia
You can call a JavaScript function and use window.location = 'url';:
您可以调用 JavaScript 函数并使用window.location = 'url';:

