当前位置:网站首页>学会iframe并用其解决跨域问题
学会iframe并用其解决跨域问题
2022-08-04 03:08:00 【M78_国产007】
了解iframe
官方定义为:iframe是HTML标签,作用是文档中的文档,或者浮动的框架(FRAME)。iframe元素会创建包含另外一个文档的内联框架(即行内框架)。
简单理解为:iframe是一个内联框架,可以在当前HTML页面中嵌入另一个文档。
iframe的属性
这里只介绍常用属性
name:规定 <iframe> 的名称。
width:规定 <iframe> 的宽度。
height:规定 <iframe> 的高度。
src:规定在 <iframe> 中显示的文档的 URL。
frameborder:HTML5 不支持。规定是否显示 <iframe> 周围的边框。属性值为1或者0,1代表有边框,0代表无边框。
scrolling:HTML5 不支持。规定是否在 <iframe> 中显示滚动条。属性值为yes、no、auto。
align:HTML5 不支持。HTML 4.01 已废弃。 规定如何根据周围的元素来对齐 <iframe>。属性值有left、right、middle、top、bottom
简单演示:
<h1>演示iframe的使用</h1>
<iframe src="http://www.bilibili.com" name="ifr" frameborder="1" height="400" width="600" scrolling="no">你的浏览器不支持该iframe标签</iframe>
我们设置了一个名为ifr,宽为600,高为400,显示边框,隐藏滑动条,显示文档为b站(也可以选择本地html文档)的内联框架。 我们可以在iframe标签中写上文字说明,因为有一些低版本浏览器不支持这个标签,显示不了文档的时候就会在页面显示我们写的文字。
打开浏览器看下效果:
获取iframe内的内容
<h1>演示iframe的使用</h1>
<iframe src="./t1.html" id="myiframe">你的浏览器不支持该iframe标签</iframe>
<script>
//获取iframe标签
var myiframe=document.querySelector("#myiframe")
//获取它的window对象
var mywindow=myiframe.contentWindow
//获取它的document对象
var mydocument=mywindow.document
</script>
解决跨域问题
1、document.domain+iframe
这个方法只能用于同一个主域下不同子域之间的跨域请求,比如a.com和1.a.com 之间,1.a.com和2.a.com 之间。
只要把两个页面的document.domian都指向主域就可以了,比如document.domain="a.com"。
父页面通过iframe嵌入子页面,通过iframe.contentWindow获取子页面的window,即可操作子页面,子页面通过parent.window和parent来访问父页面的window。
写个例子:
//父页面http://a.com/a.html
<iframe id="myiframe" src="http://1.a.com"></iframe>
<script>
document.domain="a.com"
var myiframe=document.queryselector("#myiframe")
var name1=1
//获取子页面的属性
var name22 = myiframe.contentWindow.name2;
</script>
//子页面 http://1.a.com/b.html
document.domain="a.com"
var name2=2
//获取父页面的属性
var name11=parent.window.name1
2、window.name+iframe
实现是基于window.name传递数据。name 在浏览器环境中是一个全局window对象的属性
当在 iframe 中加载新页面时,name 的属性值依旧保持不变。
边栏推荐
- TOML配置文件格式,YAML最有力的竞争者
- Big guys, it takes a long time to read mysql3 million single tables, what parameters can be discounted, or is there any way to hurry up
- Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
- base address: environment variable
- "Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
- C language -- ring buffer
- 【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
- 4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
- 初识Numpy
- [QNX Hypervisor 2.2 User Manual] 10.3 vdev gic
猜你喜欢
随机推荐
typescript type 和 interface 的区别
Why use Selenium for automated testing
数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
融云「音视频架构实践」技术专场【内含完整PPT】
Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
docker+bridge+redis master-slave+sentry mode
Pine脚本 | 如何显示和排版绘图开关?
Hey, I had another fight with HR in the small group!
自定义通用分页标签01
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
Architecture of the actual combat camp module three operations
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
LeetCode每日一题(2285. Maximum Total Importance of Roads)
Exclude_reserved_words 排除关键字
数组相关 内容 解析