开发者

International Domain Names + JQuery attr('href') problems in Firefox

开发者 https://www.devze.com 2023-04-01 22:06 出处:网络
I\'m trying to set the href of an a element using jQuery. When I set an IDN in Firefox 6, the href is url encoded. One example is the IDN http://räksmörgås.se, which is mangled to http://r%C3%A4ksm

I'm trying to set the href of an a element using jQuery. When I set an IDN in Firefox 6, the href is url encoded. One example is the IDN http://räksmörgås.se, which is mangled to http://r%C3%A4ksm%C3%B6rg%C3%A5s.se. When I do the same in other browsers (IE8, Chrome 13, Safari 5.1), the IDN isn't encoded.

The reason I am trying to do this is that i am letting the user change hrefs and then upload a subset of the page content by ajax to a web server. I then have problems since the link is buried deep in a html string; and I'd prefer not to dig 开发者_如何转开发up all a elements in the html string.

Is there any way to disable the encoding of hrefs when setting it in Firefox? Or do you have any other ways I might solve the problem?

(I've noticed that if the href is a valid punycode IDN, i.e. http://www.xn--rksmrgs-5wao1o.se, the browsers are fine and dandy. But I then have to do the punycode encoding in js, something which I prefer not to; there might be one or several bugs, and there doesn't seem to be a official release of an encoder/decoder.)

The following html page displays my problem. If you input an international domain name (copy-paste "http://www.räksmörgås.se"), #linkHtml will get the entire a element, including the mangled href if using Firefox, and the correct href if using any other browser.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script>
$(document).ready(function(){
$("#linkText").focus();
$("#linkText").keyup(function() {
    var href = $(this).val();
    $("#link").attr('href', href);
    $("#linkHtml").text($("#link").parent().html());
});
});
</script>
</head>
<body>
<div><input id="linkText" type=text /></div>
<div><a id="link" href="#">link</a></div>
<span id="linkHtml"></span>
</body>
</html>


Use decodeURI (native function) if you're concerned with how this href value is displayed. Following displays ok in Firefox.

$("#linkHtml").text(decodeURI($("#link").parent().html()));

Techincally, as far as I know, even while Firefox shows encoded value you should be ok with that data after submission to some server.

0

精彩评论

暂无评论...
验证码 换一张
取 消