pushState:如何在不重定向的情况下添加查询字符串

时间:2013-03-24 02:38:37

标签: javascript html5 url pushstate history.js

我正在研究这项技术:History API pushState(HTML5)

我需要使用命令来处理Url withput redirect,因为我有服务器的CherryPy。

我无法从Cherrypy传递数据(使用Mako模板到Javascript),但我想更改网址。

旧网址

file:///media/DATA/prototypefin4/s.html

并使用History.pushState

在Url中自动添加foo=1&foo=2(变量s)
file:///media/DATA/prototypefin4/s.html?=foo=1&foo=2

但是如何?

我的COED:

<!DOCTYPE HTML>
<html>
<head>
<title>Example - History API pushState</title>
</head>
<script src="http://balupton.github.com/history.js/vendor/jquery.js"></script>
<script src="http://balupton.github.com/history.js/scripts/bundled/html4+html5/jquery.history.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<body>

<input id = 'button1' type = 'button' value = 'history.pushState' onclick="updateHistory()"/>

<script type = 'text/javascript'>

var count ="?foo=1&foo=2";

function updateHistory()
{

 History.pushState(count,null,file:///media/DATA/prototypefin4/s.html);

}

</script>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

您的脚本在语法上无效。根据{{​​3}},以下参数是正确的:

History.pushState(null, "", "file:///media/DATA/prototypefin4/s.html"+count);