正则表达式验证和跨域postmessage

1.用正则表达式验证用户名

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form>
        <input type="text" id="username" name="username" pattern="[A-Za-z0-9_-]{6,16}" required>
        <input type="submit" value="提交">
        <div class="error-message">用户名必须由6到16个字符的字母、数字组成。</div>
      </form>
</body>
</html>

解释该正则:

  • [a-zA-Z0-9_-]:匹配任意字母、数字、下划线或破折号。
  • {3,16}:限制用户名的长度在6到16个字符之间。

2.跨域postmessage

需要用到两个虚拟主机

子页面:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2023</title>
</head>
<body>
    <div>
        <h1>2024.security.pw</h1>
    </div>
</body>
<script>
    window.addEventListener('message', (event) => {
        if (event.origin === 'http://2024.oupeng.pw') {
            const cookieData = event.data;
            //处理cookieData
            console.log('Receive message from parent:', cookieData);
            window.parent.postMessage('child message', '*');
        }
    })
</script>

</html>

父页面:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iframe</title>
</head>
<body>
    <iframe id="myIframe" src="http://2024.security.pw/" frameborder="0"></iframe>
</body>
<script>
    window.onload = function() {
        document.cookie = 'sessionid=oupeng'
        const cookieData = document.cookie
        window.frames[0].postMessage(cookieData, 'http://2024.security.pw/');
    }
    //添加一个监听事件处理子页面的返回消息
    window.addEventListener('message', (event) => {
        if(event.origin === 'http://2024.security.pw')
        console.log('Received message from child:', event.data);
    })
</script>
</html>

验证:在子页面里接受到返回值和发送消息到子页面,实现跨域

相关推荐
史努比.9 小时前
redis群集三种模式:主从复制、哨兵、集群
前端·bootstrap·html
天高任鸟飞dyz10 小时前
html加载页面
css·html·css3
miao_zz10 小时前
基于HTML5的下拉刷新效果
前端·html·html5
重生之我在20年代敲代码10 小时前
HTML讲解(一)body部分
服务器·前端·html
陈小唬11 小时前
html页面整合vue2或vue3
前端·vue.js·html
18资源11 小时前
H5白色大方图形ui设计公司网站HTML模板源码
前端·javascript·html
sqll56712 小时前
最新简洁大方的自动发卡网站源码/鲸发卡v11.61系统源码/修复版
前端·开源·html
清灵xmf12 小时前
深入解析 JavaScript 事件委托
前端·javascript·html·事件委托
2301_7969821413 小时前
网页打开时,下载的文件text/html/重定向类型有什么作用?
前端·html
重生之我在20年代敲代码13 小时前
HTML讲解(二)head部分
前端·笔记·html·web app