admin管理员组文章数量:1303439
For example if use decodeURI('%C4%97%')
it fires and error (yes, it is an error, specially for test):
URIError: malformed URI sequence ...('textarea#encode-url-result').val(decodeURI(jQuery('input#encode-url-input').va...
And even if i put it in try-catch
it still fires fatally. Is there a way to catch it and show alert?
UPDATE:
Here is my code and i still get error in console
try{
jQuery('a#encode-url-encode, a#encode-url-decode').click(function(){
if(jQuery('input#encode-url-input').val().length == 0)
showCustomAlert('<strong>Warning!</strong> Please enter value.');
var result = null;
if(jQuery(this).attr('id') == 'encode-url-encode')
result = encodeURI(jQuery('input#encode-url-input').val());
else if(jQuery(this).attr('id') == 'encode-url-decode')
result = decodeURI(jQuery('input#encode-url-input').val());
jQuery('textarea#encode-url-result').val(result);
});
}
catch(e){
alert(e);
}
For example if use decodeURI('%C4%97%')
it fires and error (yes, it is an error, specially for test):
URIError: malformed URI sequence ...('textarea#encode-url-result').val(decodeURI(jQuery('input#encode-url-input').va...
And even if i put it in try-catch
it still fires fatally. Is there a way to catch it and show alert?
UPDATE:
Here is my code and i still get error in console
try{
jQuery('a#encode-url-encode, a#encode-url-decode').click(function(){
if(jQuery('input#encode-url-input').val().length == 0)
showCustomAlert('<strong>Warning!</strong> Please enter value.');
var result = null;
if(jQuery(this).attr('id') == 'encode-url-encode')
result = encodeURI(jQuery('input#encode-url-input').val());
else if(jQuery(this).attr('id') == 'encode-url-decode')
result = decodeURI(jQuery('input#encode-url-input').val());
jQuery('textarea#encode-url-result').val(result);
});
}
catch(e){
alert(e);
}
Share
edited Apr 12, 2013 at 20:47
user1692333
asked Apr 12, 2013 at 20:34
user1692333user1692333
2,5976 gold badges34 silver badges66 bronze badges
10
- Wrong. jsfiddle/SLaks/md7s7 – SLaks Commented Apr 12, 2013 at 20:37
- How did you put it in try-catch? Try-catch doesn't work if it wraps async callback passing. – Esailija Commented Apr 12, 2013 at 20:37
- That catch will not catch anything in the callback function. You are declaring the function there, not calling it. Easiest fix is to move the try/catch inside the callback function. – Matt Greer Commented Apr 12, 2013 at 20:48
- @MattGreer it does work when the callback is called synchronously. – Esailija Commented Apr 12, 2013 at 20:49
- @Esailija No it wouldn't, that function won't get called until the url is clicked, and at that point the try/catch is long gone: jsfiddle/pgevj – Matt Greer Commented Apr 12, 2013 at 20:57
4 Answers
Reset to default 6This works fine:
try {
decodeURI('%C4%97%')
} catch (ex) {
alert("ERROR DECODING URI");
}
DEMO: http://jsfiddle/P6EBN/
EDIT:
From the looks of your error message, you're trying to set a textarea's value with jQuery.
Try something like this:
var newVal = "";
var toDecode = jQuery('input#encode-url-input').val();
try {
newVal = decodeURI(toDecode);
} catch (ex) {
// alert("ERROR DECODING URI");
}
jQuery('textarea#encode-url-result').val(newVal);
Just trying to split it up so you can target specifically the decoding.
Also, the use of a tagName
in front of an id
selector is unnecessary. Just use these selectors:
jQuery("#encode-url-input")
// and
jQuery("#encode-url-result")
Try-catch doesn't work if it wraps async callback passing.
jQuery('a#encode-url-encode, a#encode-url-decode').click(function() {
if (jQuery('input#encode-url-input').val().length == 0) showCustomAlert('<strong>Warning!</strong> Please enter value.');
var result = null;
try { //It needs to be here.
if (jQuery(this).attr('id') == 'encode-url-encode') result = decodeURI(encodeURI(jQuery('input#encode-url-input').val()));
else if (jQuery(this).attr('id') == 'encode-url-decode') result = decodeURI(decodeURI(jQuery('input#encode-url-input').val()));
} catch (e) {
//handle
}
jQuery('textarea#encode-url-result').val(result);
});
try {
var x = decodeURIComponent('%C4%97%');
} catch (ex) {
console.log("ERROR DECODING URI: " + ex.message);
}
try
{
var x = decodeURI('%C4%97%');
}
catch(e)
{
alert(e);
}
本文标签: javascriptHow to catch error when using decodeURIStack Overflow
版权声明:本文标题:javascript - How to catch error when using decodeURI? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741676513a2391912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论