• 从零开始搭建前端监控系统(三)——实现控制iframe前进后退


    前言

    本系列文章旨在讲解如何从零开始搭建前端监控系统。

    项目已经开源

    项目地址:

    • https://github.com/bombayjs/bombayjs (web sdk)
    • https://github.com/bombayjs/bombayjs-server (服务端,用于提供api)(未完)
    • https://github.com/bombayjs/bombayjs-admin (后台管理系统,可视化数据等)(未完)

    您的支持是我们不断前进的动力。

    喜欢请start!!!

    喜欢请start!!!

    喜欢请start!!!

    本文是该系列第三篇,重点讲解如何控制iframe的前进后退。

    系列文章:

    示例

    https://abc-club.github.io/demo/iframe/demo2/

    演示

    源码

    https://github.com/abc-club/demo

    如果想看跟复杂的例子,可以看bombayjs的源码

    后台截图如下:

    难点

    document.getElementById('iframe id').contentWindow.history.back();
    

    以上面这种方式控制会存在跨域问题!!!

    原理

    1. 解决iframe的跨域问题,我们需要通过postMessage实现iframe的通信
    2. 通过window.history.back()和window.history.forward()控制前进后退

    实现

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
      <div>
        <iframe id='iframe'></iframe>
        <br/>
        url: <span id='url'></span>
        <br/>
        <button id='back' onclick='back()'>back</button>
        <button id='forward' onclick='forward()'>forward</button>
    </div>
      <script>
        var url = './iframe.html'
        var div = document.getElementById('url'),
            iframe = document.getElementById('iframe')
        iframe.src = url
        div.innerHTML = url
        window.addEventListener('message', function(event) {
          if (!event.data.url) return
          div.innerHTML = event.data.url;
        }, false)
    
    
        function back() {
          iframe.contentWindow.postMessage('back', '*');
        }
    
        function forward() {
          iframe.contentWindow.postMessage('forward', '*');
        }
    
      </script>
    </body>
    </html>
    
    
    

    iframe.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
      <div>
        <a href='#a'>to #a</a>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p>1</p>
        <p id='a'>a</p>
        <p>2</p>
        <p>2</p>
        <p>2</p>
        <p>2</p>
        <p>2</p>
        <p>2</p>
        <p>2</p>
        <p>2</p>
    </div>
      <script>
        window.addEventListener('message', function(event) {
          if (event.data === 'back') {
            window.history.back()
          } else {
            window.history.forward()
          }
        }, false)
    
    
        window.addEventListener('hashchange', function(event) {
          window.parent.postMessage({
            url: location.href
          }, '*')
          return
        }, false)
        
      </script>
    </body>
    </html>
    

    更多资源

    https://github.com/abc-club/free-resources


    本篇文章由一文多发平台ArtiPub自动发布

  • 相关阅读:
    Web Api跨域访问配置及调用示例
    EasyUI datagrid 日期时间格式化
    bootstrap-table组合表头
    Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web mod
    Java 开发环境配置
    一台电脑上配置多个tomcat
    使用plsql Developer 连接远程服务器
    阿里云初识
    算法入门———冒泡排序
    算法入门———递归
  • 原文地址:https://www.cnblogs.com/aoping/p/11722708.html
Copyright © 2020-2023  润新知