1

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);
    $.address.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?

Gab
  • 2,216
  • 4
  • 34
  • 61

4 Answers4

4

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/

Mrchief
  • 75,126
  • 20
  • 142
  • 189
0

substring(1) means to start at the 2nd character. substring(from,to). So IE is not returning the initial "/".

fanfavorite
  • 5,128
  • 1
  • 31
  • 58
0

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

Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
0

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
}
MikeM
  • 27,227
  • 4
  • 64
  • 80