js一些常用工具代码

发布时间:2021-11-23 15:16:47 阅读:(138)

    js监听页面的访问与折叠(用户离开页面和用户回到页面)

    !function(){    
        document.addEventListener('visibilitychange',function(){
            let pageVisibility = document.visibilityState
            if (pageVisibility === 'hidden'){
                console.log('hidden')        
            }        
            if (pageVisibility === 'visible'){
            console.log('visible')            
            // document.location.reload()        
            }    
    })}
    

    js监听视频播放完成

        var video = document.getElementById("video")
        video.addEventListener("ended",function(){
            console.log('video ended')
            document.getElementById("video_div").style.display = "none"
            // 视频播放完成,展示表单
            document.getElementById("form_div").style.display = ''
        })
    

    js向表单中插入新的数据,并控制表单提交

    function formSubmit(){
        let form = $('#form')
        let serialInput = $("<input type='hidden' name='serial_code'>")                       
        serialInput.attr('value',this.getQueryVariable('serial_code'))
        let verifyInput = $("<input type='hidden' name='verification_code'>")    
        verifyInput.attr('value',this.getQueryVariable('verification_code'))    
        form.append(serialInput)
        form.append(verifyInput)
        console.log(form)
        form.submit();
    }
    

    js获取请求中的参数

    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] === variable){return pair[1];}
        }
        return(false);
    }