我正在尝试使用_GET方法动态获取查询参数。
我的功能看起来像这样:
var baseURL = "http://example.org";
// clears the last query params
history.pushState("", document.title, baseURL);
// retrieves the value from the HTML code
var value = document.getElementById('id').value;
// URL with updated query params
var updatedURL = document.URL + "?value=" + value;
// pushing the new URL without refreshing the page.
history.pushState("", document.title, updatedURL);
// URL looks something like this "http://example.org?value=1"
But when I try to use _GET['value']
to retrieve the value from the URL, it only gets the value the page initialized with and does not update dynamically, is there any way to retrieve this value without having to refresh the page?
history.pushState()
simply changes the value of the browser URL. It doesn't actually make a server request and load the _GET variables. That's why you can't access them.要做到这一点而不刷新页面,您将需要进行ajax调用。