jQuery History Plugin replacing URL wrong
So I am using the jQuery History plugin (http://tkyk.github.com/jquery-history-plugin/). Also, I wrote the script below to switch through pages.
$(document).ready(functi开发者_运维技巧on() {
// Define the default pagetitle.
var defaultTitle = "MySite / ";
// Set a default pagetitle.
document.title = defaultTitle + "Homepage";
// Switch the page.
function load(page) {
$("#contentHolder").fadeOut("fast",function(){
$('#contentHolder').load("content/"+ page +".php");
$("#contentHolder").fadeIn("fast");
});
}
// Function to shorten the scriptsize.
function reduceSize(mainUrl) {
// Define the URL.
var url = mainUrl;
// Replace the URL in the address bar.
url = url.replace(/^.*#/, '');
// Load the page.
$.history.load(url);
return false;
}
// Enable history.
$.history.init(function(url) {
load(url == "" ? "general/homepage" : url);
});
// Make the menu useful.
$('.switch_homepage').live('click', function(e) {
reduceSize("general/homepage");
document.title = defaultTitle + "Homepage";
});
});
My problem is that whenever I click the "Homepage" button, the page switches to: http://www.domain.com/#general%2Fhomepage while it should switch to http://www.domain.com/#general/homepage
Can anybody figure out why it converts the slash to an HTML format?
Thanks.
Looking at the plugin source (https://raw.github.com/tkyk/jquery-history-plugin/master/jquery.history.js), it looks like it's doing the encoding for you.
function initObjects(options) {
options = $.extend({
unescape: false
}, options || {});
locationWrapper.encoder = encoder(options.unescape);
function encoder(unescape_) {
if(unescape_ === true) {
return function(hash){ return hash; };
}
if(typeof unescape_ == "string" &&
(unescape_ = partialDecoder(unescape_.split("")))
|| typeof unescape_ == "function") {
return function(hash) { return unescape_(encodeURIComponent(hash)); };
}
return encodeURIComponent;
}
function partialDecoder(chars) {
var re = new RegExp($.map(chars, encodeURIComponent).join("|"), "ig");
return function(enc) { return enc.replace(re, decodeURIComponent); };
}
}
You can try setting an option of unescape: true
in your initialization, see if that works.
精彩评论