• Flash与JavaScript互动


    最近做的一个项目需要用javascript来实现自动复制文本到剪切板,但测试时发现只有ie6.0支持。

    到百度搜索后才发现,原来ie7.0、firefox是不支持这样的操作的,随后又搜索了一下,找到一个解决办法:

    通过javascript调用Flash来复制文本到剪切板。

    在HTML代码中需注意事项:

    在OBJECT标签中,插入id="fcopy"
    在EMBED标签中,插入name="fcopy"和swLiveConnect="true",确保没有使用id属性!

    Flash代码:

    //导入ExternalInterface类
    import flash.external.ExternalInterface;
    function copy(str){
    System.setClipboard(str);
    getURL("javascript:alert('已复制,请使用Ctrl+V粘贴出来')");
    }


    //允许js中的flAlert()调用flash中的showAlert()
    ExternalInterface.addCallback("copy",null,copy);

    HTML代码:

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" id="fcopy" width="1" height="1">
    <param name="movie" value="flash/clipboard.swf">
    <param name="quality" value="high">
    <param name="swLiveConnect" value="true">
    <embed src="flash/clipboard.swf" width="1" height="1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="fcopy" swLiveConnect="true"></embed>
    </object>

    js代码:

    var browser = new Object();
    function getBrowser() {
    var b = navigator.userAgent.toLowerCase();
    browser = {
       safari: /webkit/.test(b),
       opera: /opera/.test(b),
       ie6: /msie 6/.test(b) && !/opera/.test(b),
       ie7: /msie 7/.test(b) && !/opera/.test(b),
       msie: /msie/.test(b) && !/opera/.test(b),
       mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
    };
    }
    getBrowser() ;
    
    //复制到剪切板
    function copyCode(field) {
    
        field.select();
    
    var text=field.value;
    
    if(browser.ie6) {
       window.clipboardData.setData('text',text)
       alert('已复制,请使用Ctrl+V粘贴出来') ;
    }
    else
    {
       flashCopy(text); //采用flash方式复制
    }
    }
    
    //调用flash中的方法,"fcopy"为html页中swf的id
    function flashCopy(text) {
    thisMovie("fcopy").copy(text);
    }
    //搭建js与flash互通的环境
    function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName]
    }else{
    return document[movieName]
    }
    }
  • 相关阅读:
    HtmlAgilityPack
    随笔-20150513
    过滤掉html 标签
    json转换
    第28月第3天 c语言读写文件
    第27月第28天 iOS bundle
    第27月第27天 https
    第27月第25天 clang -rewrite-objc main.m
    第27月第24天 git pull fetch
    第27月第18天 epoll lt et
  • 原文地址:https://www.cnblogs.com/personnel/p/4582941.html
Copyright © 2020-2023  润新知