In order to load a page with ajax I have the following in my script file:
$(".ajaxed").live("click", function(event) {
var post_slug = $(this)[0].pathname.substring(1);
alert(post_slug);
$.addres开发者_运维百科s.crawlable(true).value(post_slug);
$("#board").load("ajax/",{slug:post_slug});
return false;
});
When the user clicks on an anchor linking to http://www.website.com/link1 the post_slug alert is link1. But when I use this in IE8 the post_slug alert is ink1 instead of link1. What am I doing wrong ?
I guess it's .substring(1) but what can I do?
You can use this:
$(this)[0].pathname.replace("/", "");
Tested on IE7, Chrome: http://jsfiddle.net/mrchief/vB2Fu/3/
You can make the replace bit more careful by using regex to replace only the starting slash
$(this)[0].pathname.replace(/^\//, "");
Update:
For nested slugs, I changed it a bit:
$(this)[0].pathname.substring($(this)[0].pathname.lastIndexOf("/")).replace(/^\//, "");
Demo (tested on IE7, Chrome): http://jsfiddle.net/mrchief/vB2Fu/5/
substring(1) means to start at the 2nd character. substring(from,to). So IE is not returning the initial "/".
Your problem is that IE returns a different value for pathname
(without the leading /
).
You can either test for it and use/omit substring
accordingly or get the whole URL and use split
could test for it
if ($(this)[0].pathname.substring(0, 1) === "/") {
post_slug = $(this)[0].pathname.substring(1);
} else {
post_slug = $(this)[0].pathname; // ie8 oddness
}
精彩评论