获取当前域名并生成二维码

1.获取当前域名并生成二维码提供下载

1这是全代码 ,如果要获取url的话直接修改window.location.host 把host改为url,或者href

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>js_canvas_download</title>
<script type='text/javascript' src='http://cdn.staticfile.org/jquery/3.4.1/jquery.min.js'></script>
<script type="text/javascript" src="http://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>

</head>
<body>
<div id="qrcode"></div>
<a id="download" download="qrcode.jpg"></a>
<button id="save">save</button>
</body>
</html>
<script>
jQuery('#qrcode').qrcode({ width: 100, height: 100, text: window.location.host });
$("#save").click(function () {
var canvas = $('#qrcode').find("canvas").get(0);
var url = canvas.toDataURL('image/jpeg');
$("#download").attr('href', url).get(0).click();
return false;
});
</script>

2,如果要关闭下载二维码的话就直接注释掉 id=save

2.1,如果想要生成的二维码居中的话,就可以定义下qrcode,在<head> 和</head>之间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<style>
#qrcode{
display: flex;
justify-content: center;
margin: 0 auto;
}
.dd{
display: flex;
justify-content: center;
margin: 5px auto;
}
</style>

# .dd是定义的文字居中,
<div class="dd">
</dt>
<dd >手机扫码下载</dd>
</dl>
</div>

3, 如果此网页正在试图从未经验证的来源加载脚本

aro0S0.png

就把连接添加https,还可以通过F12来查看是什么代码导致了这种情况

arorOU.png

会发现是http://pet.zoosnet.pet是这条有问题,把注释掉就可以了,或者添加安全连接,

2, 获取固定域名的二维码

首先需要用到一个qrcode.js 库 git clone https://github.com/davidshimjs/qrcodejs.git

以下是示列:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

<html>
<head>
<meta charset="utf-8">
<title> 练习 </title>
<script type="text/javascript" src="html/js/qrcodejs/qrcode.min.js"></script>
<script type='text/javascript' src='https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js'></script>
</head>
<body>
<div id="qrcode"></div>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
text: "https://wohenliu.com",
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
</script>

</body>

</html>

然后就自动生成二维码了

arThNj.png

评论


:D 一言句子获取中...

加载中,最新评论有1分钟缓存...