Unable to set correct href link in IE with jquery address
Using jquery address I'm setting my href links like so:
<a href="#view_profile=123">view 123</a>
When calling my address change event:
$.address.change(function(e) {
alert(e.value);
});
I s开发者_如何学JAVAee values from FF and Chrome as I would expect:
/view_profile=123
IE however returns the full URL path with a preceding "/" like so:
/http://localhost/#view_profile=123
Any idea why IE does this and what's the best way to fix it? I've tried several things but it's the same every time.
Here is the code that I use to get the link path:
// Setup jQuery address on some elements
$('a').address();
// Run some code on initial load
$.address.init(function(e) {
// Address details can be found in the event object
});
// Handle any URL change events
$.address.change(function(e) {
alert(e.value);
var urlAux = e.value.split('=');
var page = urlAux[0];
var arg = urlAux[1];
alert("page: " + page);
alert("arg: " + arg);
if (page == "/view_profile") {
...
}
});
I found a work around. If I add a class tag to the link and look for a click event on the class I can set the URL and it triggers the jquery address change event correctly.
$(".testclass").click(function() {
var id = null;
if ($.browser.msie) {
id = $(this).attr('href').slice($(this).attr('href').indexOf('#')+1);
}
else {
id = $(this).attr('href').substr(1);
}
alert("ID: " + id );
location.href = "#view_profile=" + id;
return false;
});
I also had to set the href value to the ID like so:
<a href="#123" class="testclass">123</a>
try using $(this).prop("href") to get the url. it should get you the same value in all browsers.
Edit: pathname doesn't work in IE.
.. guess i should have done more testing. pathname doesn't work in IE. just use "href"
精彩评论