12/06/2009

URL線上編碼/解碼器

完整JavaScript程式碼如下所示:
<html><head></head>
<body>
<select size="1" id="url_encoding_type" onChange="changeButtonText();">
<option value="1">編碼</option>
<option value="2">解碼</option>
</select>

<input type="button" id="encoding_button" onClick="showResult();" value="URL編碼" /><br>

<textarea id="source_text" rows=6 onMouseOver="checkText();" onMouseOut="checkBlankText();">請置入URL字串</textarea><br>

<textarea id="target_text" rows=6 onClick="selectAll();"></textarea>

<script type="text/javascript">
function changeButtonText() {
document.getElementById("encoding_button").value="URL"+document.getElementById("url_encoding_type").options[document.getElementById("url_encoding_type").selectedIndex].text;
}

function showResult() {
if (document.getElementById("source_text").value!="請置入URL字串" && document.getElementById("source_text").value!="") {
if (document.getElementById("url_encoding_type").value=="1") {
document.getElementById("target_text").value=encodeURI(document.getElementById("source_text").value);
}
else {
document.getElementById("target_text").value=decodeURI(document.getElementById("source_text").value);
}
}
else {alert("請先置入URL字串後,再按"+document.getElementById("encoding_button").value+"鈕!");}
}

function checkText() {
if (document.getElementById("source_text").value=="請置入URL字串") {
document.getElementById("source_text").value="";
document.getElementById("source_text").style.color="#000000";
}
}

function checkBlankText() {
if (document.getElementById("source_text").value=="") {
document.getElementById("source_text").value="請置入URL字串";
document.getElementById("source_text").style.color="#999999";
}
}

function selectAll() {
if (document.getElementById("target_text").value!="") {
document.getElementById("target_text").focus();
document.getElementById("target_text").select();
window.clipboardData.setData("Text", document.getElementById("target_text").value);
alert(document.getElementById("encoding_button").value+"字串已完成複製");
}
}
</script>
</body>
</html>
添加到收藏夾 / 分享

沒有留言:

張貼留言