iframe

  1. <iframe src="./index2.html" id="myIframe"></iframe>
  2. <script>
  3. var myIframe=document.getElementById("myIframe");
  4. myIframe.onload=function (){
  5. console.log(myIframe.contentWindow.name);
  6. }
  7. </script>
  1. <script !src="">
  2. // window.name='iframeWindow';
  3. // name='iframeWindow';
  4. var name='iframeWindow';
  5. </script>

运行index结果如下
image.png
image.png
当index2被index的iframe元素加载时,可以认为index get得到的iframe有index2的window对象,iframe是一个窗口,iframe.contentWindow等价于index2的window窗口对象(窗口对象)。window.name,就相当于声明了一个变量。index2中的三句话是一个意思。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index</title>
  6. </head>
  7. <body>
  8. <iframe src="./index2.html" id="myIframe"></iframe>
  9. <script>
  10. var myIframe = document.getElementById("myIframe");
  11. window.name = 'mainWindow';
  12. myIframe.onload = function () {
  13. console.log(myIframe.contentWindow.name);
  14. }
  15. </script>
  16. </body>
  17. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index2</title>
  6. </head>
  7. <body>
  8. <iframe src="./index3.html" id="myIframe"></iframe>
  9. <script !src="">
  10. window.name = 'iframeWindow';
  11. var iframe = document.getElementById('myIframe');
  12. iframe.onload = function () {
  13. console.log(window.parent.name)
  14. }
  15. </script>
  16. </body>
  17. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index3</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>

index运行结果
image.png
index2运行结果
image.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index</title>
  6. </head>
  7. <body>
  8. <iframe src="./index2.html" id="myIframe"></iframe>
  9. <script>
  10. var myIframe = document.getElementById("myIframe");
  11. window.name = 'mainWindow';
  12. myIframe.onload = function () {
  13. console.log('index contentWindow',myIframe.contentWindow.name);
  14. }
  15. </script>
  16. </body>
  17. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index2</title>
  6. </head>
  7. <body>
  8. <iframe src="./index3.html" id="myIframe"></iframe>
  9. <script !src="">
  10. window.name = 'iframeWindow';
  11. var iframe = document.getElementById('myIframe');
  12. iframe.onload = function () {
  13. console.log('index2 parent',window.parent.name)
  14. }
  15. </script>
  16. </body>
  17. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index3</title>
  6. </head>
  7. <body>
  8. <script !src="">
  9. console.log('index3 parent',window.parent.name)
  10. console.log('index3 parent.parent',window.parent.parent.name)
  11. </script>
  12. </body>
  13. </html>

index运行结果
image.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index</title>
  6. </head>
  7. <body>
  8. <script>
  9. window.name = "window";
  10. location.href='index2.html';
  11. </script>
  12. </body>
  13. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index2</title>
  6. </head>
  7. <body>
  8. <script !src="">
  9. console.log('index',window.name);
  10. </script>
  11. </body>
  12. </html>

image.png
window.name有共享性的,换成name1就变成了undefined,即使不同页面也可以共享

image.png

  1. <iframe src="http://test2.jsplusplus.com/transfer/index.html" id="myIframe"></iframe>
  2. <script !src="">
  3. var myIframe=document.getElementById("myIframe");
  4. myIframe.onload=function (){
  5. console.log(myIframe.contentWindow.name);
  6. }
  7. </script>

image.png
跨域错误,主页面源不同,无法拿到window

跨域HTTP请求

跨域
http://test2.jsplusolus.com/向源[http://test2.jsplusolus.com](http://test2.jsplusolus.com)获取资源

6种跨域方法

方法1 服务器中转请求

image.png

2 设置基础域名+iframe

image.png
index1

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>index</title>
  6. </head>
  7. <body>
  8. <script !src="">
  9. //设置基础域名
  10. document.domain = 'jsplus.com'; //**
  11. //创建了iframe
  12. var iframe = document.createElement('iframe');
  13. // iframe 引入与接口同源的的页面 1.设置相同的基础域名
  14. //iframe引入不同源页面
  15. iframe.src = 'http://test.jsplus.com/index.html';
  16. iframe.id = 'myIframe';
  17. //设置为none,不让其显示,但是加载运行了,只是视觉效果看不到
  18. iframe.style.display = 'none';
  19. iframe.onload = function () {
  20. console.log(document.getElementById('myIframe').contentWindow);
  21. }
  22. document.body.appendChild(iframe);
  23. </script>
  24. </body>
  25. </html>

image.png

index2

  1. //设置基础域名
  2. document.domain = 'jsplus.com'; //**
  3. var iframe = document.createElement('iframe');
  4. // iframe 引入与接口同源的的页面 1.设置相同的基础域名
  5. iframe.src = 'http://test.jsplus.com/index.html';
  6. iframe.id = 'myIframe';
  7. iframe.style.display = 'none';
  8. iframe.onload = function(){
  9. // 获取并使用iframe的ajax程序
  10. var $$ = document.getElementById('myIframe').contentWindows.$;
  11. $$.post('http://test.jsplus.com/get_courses1.php',{
  12. status:1,
  13. },function(data){
  14. console.log(data);
  15. });
  16. }
  17. document.body.appendChild(iframe);

被引入的请求页面 http://test.jsplus.com/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>index2</title>
  8. </head>
  9. <body>
  10. </body>
  11. <script src="./utils.js"></script>
  12. <script>
  13. //设置基础域名
  14. document.domain = 'jsplus.com';
  15. </script>
  16. </html>

封装ajaxDomain函数

  1. var ajaxDomain = (function () {
  2. //在返回的函数中被调用,参数被赋值
  3. function createIframe(frameId, frameUrl) {
  4. var frame = document.createElement('iframe');
  5. frame.scr = frameUrl;
  6. frame.id = frameId;
  7. frame.style.display = 'none';
  8. return frame;
  9. }
  10. return function (opt) {
  11. document.domain = opt.basicDomain;
  12. var frame = createIframe(opt.frameId, opt.frameUrl);
  13. frame.onload = function () {
  14. var $$ = document.getElementById(opt.frameId).contentWindows.$;
  15. $$.ajax({
  16. url: opt.url,
  17. type: opt.type,
  18. data: opt.data,
  19. success: opt.success,
  20. error: opt.error
  21. })
  22. }
  23. document.body.appendChild(frame);
  24. }
  25. })();
  26. console.log(window)
  27. ajaxDomain({
  28. basicDomain: 'jsplus.com',
  29. frameUrl: 'http://test.jsplus.com/index.html', //需要引入的请求的页面
  30. url: 'http://test.jsplus.com/get_courses1.php', //需要请求的接口
  31. type: 'POST',
  32. data: {
  33. status: 1
  34. },
  35. success: function (data) {
  36. console.log(data);
  37. },
  38. error: function () {
  39. console.log('error');
  40. }
  41. })8