我有一个问题: 我想通过JavaScript重定向到上面的目录。 我的代码:
location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'));
网址如下所示:
example.com/path/folder/index.php?file=abc&test=123&lol=cool
重定向会影响到这一点:
example.com/path/&test=123&lol=cool
但是想拥有这个:
example.com/path /
我怎么能这样做?
答案 0 :(得分:587)
您可以进行相对重定向:
window.location.href = '../'; //one level up
或
window.location.href = '/path'; //relative to domain
答案 1 :(得分:12)
如果您使用location.hostname
,您将获得domain.com部分。然后location.pathname
会给你/ path /文件夹。我会将location.pathname
拆分为/并重新组合URL。但除非您需要查询字符串,否则只需重定向到..
即可进入上面的目录。
答案 2 :(得分:8)
重定向到../
答案 3 :(得分:7)
<a href="..">no JS needed</a>
..
表示父目录。
答案 4 :(得分:3)
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
function persistence(num) {
// Create a new function that you'll use inside your main function
function multiply(n) {
// Multiply the first number by next number in the array
// until the entire array has been iterated over
return n.reduce(function(a,b){return a*b;});
}
// Set the count to 0
var count =0;
// Use a while loop to iterate the same number of times
// as there are digits in num
while (num.toString().length > 1) {
// Splits the num into an array
num= num.toString().split("");
// Runs multiply on our num array and sets num to the returned
// value in preparation of the next loop.
// The multiply function will for 39 run 3 * 9 = 27,
// next iteration we've set num to 27 so multiply will be
// 2 * 7 = 14, then 1 * 4 = 4, and since num 4 now
// has a length <= 1 the loop stops.
num = multiply(num);
// Increase count by 1 each iteration, then run the next
// iteration in the loop with the new value for num being
// set to the result of the first multiplication.
count++;
}
return count; // return the value of count
}
console.log(persistence(39));// === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
console.log(persistence(999));// === 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
console.log(persistence(4));// === 0 // because 4 is already a one-digit number
//一级上升window.location.assign("../");
//相对于域名答案 5 :(得分:1)
我正在尝试使用JavaScript将我当前的网站重定向到同一页面上的其他部分。以下代码对我有用:
location.href='/otherSection'
答案 6 :(得分:0)
尝试执行以下js代码
location = '..'