Javascript - 获取 location.pathname + location.search - 不占用所有部
Javascript - get location.pathname + location.search - not taking all parts
我正在编写接受实际location.pathname + location.seach
的js函数,以便用户在点击back
按钮时可以返回搜索结果页面。
示例网址:http://127.0.0.1:8000/search_for_book/?titel=&autor=doniyor#
function select_book(bookid){
var backurl = String(window.location.pathname+window.location.search);
//alert(backurl); //<---- this is giving the correct full path
window.location = 'selected/?book_id=' + bookid + '&back=' + backurl;
}
但在最后一行中,函数只附加到?titel=
并切断其余部分。 新的 URL 变成了这样:
http://127.0.0.1:8000/search_for_book/selected/?book_id=10&back=/search_for_book/?titel=
为什么会这样?我需要完整的Location.pathname和完整的Location.search。
有什么想法吗?
但在最后一行中,函数只附加到 ?titel= 并切断其余部分。
不,它没有。提醒/记录您分配给 window.location 的字符串值会向您显示这一点。(顺便说一句,window.location.href
是更新位置的正确方法。 location
本身是一个对象,而不是一个属性——只有浏览器的容错能力才能让你这样做。因此,请改用window.location.href = …
。
这是错误的,因为 URL 中的&
将参数彼此分开。因此,参数的值back
在&
之后结束,然后出现一个新的参数autor
- 因为您忽略了正确对参数值进行URL编码。
在将值添加到字符串之前,对值使用 encodeURIComponent
。
相关文章:
- $location.search() - 如何在 $scope.$watch 函数中使用作用域中的参数
- $location.search() 与外部函数一起使用,重置整个$scope.如何避免它
- 使用 window.location.search 时,URL 中的字符会更改
- JavaScript window.location.search.substr(1)以空字符串形式返回
- Javascript - 获取 location.pathname + location.search - 不占用所有部
- 如果 JavaScript 错过了 Location.Search,则显示警报消息
- Jquery get window.location.search tag in array
- $location.search() 不起作用 - AngularJs 1.5
- 尝试将 href 属性和 location.search 结合起来
- AngularJS $location.search
- window.location.href or window.location.search
- Angular$location.search替换浏览器历史记录
- 如何在不更改$location.search中params值的情况下触发routeUpdate
- javascript使用location.search获取url不起作用
- 将location.pathname和location.search一起设置
- element.click和$location.search的棱角分明的怪异
- 如何在尝试替换$location.search时避免转义
- 为什么window.location.search是空的?
- 当我通过location.search搜索中文时,奇怪的字符
- $location.search在前面加上散列URL时为空