<input name='url' id='url' type='text' placeholder="input" />
<input id="submit" type="button" value="sumbit" onclick="APP.dwz.setUrl(this)" />
//样式代码
.....
//样式代码结束
<script type="text/javascript">
var APP = (function() {
var dwz = {
setUrl: function(self) {
var urlEl = document.getElementById('url'),
tips = 'https://',
request = {
url: urlEl.value
};
dwz.getJson('api.php', true, dwz.build_query(request), function(res) {
if (res.code == 0) {
urlEl.className = 'focus';
urlEl.value = res.dwz;
} else {
urlEl.className = '';
urlEl.value = '';
urlEl.setAttribute('placeholder', res.msg);
setTimeout(function() {
urlEl.setAttribute('placeholder', tips);
}, 5000);
}
});
},
build_query: function(obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
},
// 获取 JSON 数据
getJson: function(url, post, data, callback) {
var xhr = new XMLHttpRequest(),
type = (post) ? 'POST' : 'GET';
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var json = JSON.parse(xhr.responseText);
callback(json);
} else if (xhr.readyState == 4) {
callback(false);
}
}
xhr.open(type, url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
},
init = function() {
setTimeout(function() {
var el = document.getElementsByTagName('html')[0];
el.className = 'on';
}, 10);
};
return {
dwz: dwz,
init: init
}
})();
document.addEventListener('DOMContentLoaded', function() {
APP.init();
})
</script>
代码如上,希望在点击按钮生成短网址之后可以通过 phpqrcode.php 同时生成该短网址的二维码