Skip to content

URL preserving option #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
// pjax specific options:
//
//
// container - Where to stick the response body. Usually a String selector.
// $(container).html(xhr.responseBody)
// (default: current jquery context)
// push - Whether to pushState the URL. Defaults to true (of course).
// replace - Want to use replaceState instead? That's cool.
// container - Where to stick the response body. Usually a String selector.
// $(container).html(xhr.responseBody)
// (default: current jquery context)
// push - Whether to pushState the URL. Defaults to true (of course).
// replace - Want to use replaceState instead? That's cool.
// preserveURL - Whether to preserve ULR until response comes.
//
// For convenience the second parameter can be either the container or
// the options object.
Expand Down Expand Up @@ -147,10 +148,11 @@ function handleSubmit(event, container, options) {
//
// Accepts these extra keys:
//
// container - Where to stick the response body.
// $(container).html(xhr.responseBody)
// push - Whether to pushState the URL. Defaults to true (of course).
// replace - Want to use replaceState instead? That's cool.
// container - Where to stick the response body.
// $(container).html(xhr.responseBody)
// push - Whether to pushState the URL. Defaults to true (of course).
// replace - Want to use replaceState instead? That's cool.
// preserveURL - Whether to preserve ULR until response comes.
//
// Use it just like $.ajax:
//
Expand Down Expand Up @@ -352,7 +354,8 @@ function pjax(options) {
// Cache current container element before replacing it
cachePush(pjax.state.id, context.clone().contents())

window.history.pushState(null, "", stripPjaxParam(options.requestUrl))
var url = options.preserveURL ? undefined : stripPjaxParam(options.requestUrl)
window.history.pushState(null, "", url)
}

fire('pjax:start', [xhr, options])
Expand Down
22 changes: 22 additions & 0 deletions test/unit/fn_pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ if ($.support.pjax) {
frame.$("a[href='/dinosaurs.html']").click()
})

asyncTest("pushes new url right after sending request", function() {
var frame = this.frame

frame.$("#main").pjax("a").on("pjax:send", function() {
equal(frame.location.pathname, "/dinosaurs.html")
start()
})

frame.$("a[href='/dinosaurs.html']").click()
})

asyncTest("preserves current url", function() {
var frame = this.frame

frame.$("#main").pjax("a", {preserveURL: true}).on("pjax:send", function() {
equal(frame.location.pathname, "/home.html")
start()
})

frame.$("a[href='/dinosaurs.html']").click()
})

asyncTest("replaces container html from response data", function() {
var frame = this.frame

Expand Down
27 changes: 27 additions & 0 deletions test/unit/pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ if ($.support.pjax) {
})
})

asyncTest("pushes new url right after sending request", function() {
var frame = this.frame

frame.$('#main').on('pjax:send', function() {
equal(frame.location.pathname, "/env.html")
start()
})
frame.$.pjax({
url: "env.html",
container: "#main"
})
})

asyncTest("preserves current url", function() {
var frame = this.frame

frame.$('#main').on('pjax:send', function() {
equal(frame.location.pathname, "/home.html")
start()
})
frame.$.pjax({
url: "env.html",
container: "#main",
preserveURL: true
})
})

asyncTest("replaces container html from response data", function() {
var frame = this.frame

Expand Down