NoO*_*oOb 5 javascript ajax jquery
我不能直接复制生成的链接(没有ctrl + C)我是usign document.execCommand('copy')但它似乎没有效果.
如果代码没有AJAX那么它的工作就完美了.这是
HTML:
<div class="permalink-control"> </div>
Run Code Online (Sandbox Code Playgroud)
JQUERY:
$(".permalink-control")
.append(
'<div class="input-group">' +
' <span class="input-group-btn"><button type="button" class="btn btn-default" title="Get Permalink"><span class="glyphicon glyphicon-link"></span></button></span>' +
' <input type="text" class="form-control">' +
'</div>'
);
$(".permalink-control input")
.hide()
.focus(function () {
// Workaround for broken selection: https://stackoverflow.com/questions/5797539
var $this = $(this);
$this.select()
.mouseup(function () {
$this.unbind("mouseup");
return false;
});
});
$(".permalink-control button")
.click(function () {
var $this = $(this);
$.ajax({
url: "https://api-ssl.bitly.com/shorten",
dataType: "jsonp",
data: {
longUrl: window.location.href,
access_token: "your access token",
format: "json"
},
success: function (response) {
var longUrl = Object.keys(response.results)[0];
var shortUrl = response.results[longUrl].shortUrl;
if (shortUrl.indexOf(":") === 4) {
shortUrl = "https" + shortUrl.substring(4);
}
$this.parents(".permalink-control")
.find("input")
.show()
.val(shortUrl)
.focus();
},
async:false
});
});
Run Code Online (Sandbox Code Playgroud)
更新:
没有回答我的问题,因为如果没有AJAX,我的代码也会在没有使用ctrl + C的情况下进行复制.但是当我使用AJAX时document.execCommand('copy')无法正常工作.
在W3规范中明确说明了这一点的原因:
如果从用户信任和触发的事件调度事件,或者实现配置为允许此操作,则通过脚本API触发的复制和剪切命令将仅影响实际剪贴板的内容.
但是,说过我们可以尝试通过复制文本来欺骗浏览器when a user does some interaction.
在这种情况下,因为您正在寻找一个click事件,我假设您是用户正在与之交互mouse
那么,如果我在ajax调用解决后附加一个
$(window).blur()或一个$(document).click()事件怎么办?
这是正确的,因为用户必须blur在某些时候使用副本选择,用户将启动一个blur() or click() (depending on your need),我们可以将文本复制到剪贴板.
$(document).ready(function(){
var shortUrl;
$(".permalink-control")
.append(
'<div class="input-group">' +
' <span class="input-group-btn"><button type="button" class="btn btn-default" title="Get Permalink"><span class="glyphicon glyphicon-link"></span></button></span>' +
' <input type="text" class="form-control">' +
'</div>'
);
$(".permalink-control input")
.hide()
.focus(function () {
// Workaround for broken selection: http://stackoverflow.com/questions/5797539
var $this = $(this);
$this.select();
document.execCommand('copy');
$this.mouseup(function () {
$this.unbind("mouseup");
return false;
});
});
$(".permalink-control button")
.click(function () {
var shortUrl ="";
var $this = $(this);
$.ajax({
url: "https://api-ssl.bitly.com/shorten",
dataType: "jsonp",
data: {
longUrl: window.location.href,
access_token: "48ecf90304d70f30729abe82dfea1dd8a11c4584",
format: "json"
},
success: function (response) {
var longUrl = Object.keys(response.results)[0];
shortUrl = response.results[longUrl].shortUrl;
if (shortUrl.indexOf(":") === 4) {
shortUrl = "https" + shortUrl.substring(4);
}
$this.parents(".permalink-control")
.find("input")
.show()
.val(shortUrl)
.focus();
}
}).done(function(){
$(window).blur(function(){
document.execCommand('copy');
$(window).off('blur');// make sure we don't copy anything else from the document when window is foucussed out
});
})
});
})Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="permalink-control"></div>
<div class"log"></div>Run Code Online (Sandbox Code Playgroud)
PS:这已经过镀铬测试.
| 归档时间: |
|
| 查看次数: |
5308 次 |
| 最近记录: |