在视图源代码中阻碍 Js 不起作用

Hinding Js in Viewsource is not working

本文关键字:Js 不起作用 视图 源代码      更新时间:2023-09-26

我需要什么

  • 我需要在视图源代码中隐藏 js 代码JS代码

        function unloadJS(scriptName) {
        var head = document.getElementsByTagName('head').item(0);
        var js = document.getElementById(scriptName);
        js.parentNode.removeChild(js);
        }
        function unloadAllJS() {
        var jsArray = new Array();
        jsArray = document.getElementsByTagName('script');
        for (i = 0; i < jsArray.length; i++){
        if (jsArray[i].id){
        unloadJS(jsArray[i].id)
        }else{
        jsArray[i].parentNode.removeChild(jsArray[i]);
        }
        }       
        }
    
            var page_count = {{count()}};
            if (page_count == 4)
            {
            dataLayer.push({'event':'mobilePromo-android'});
            }
        $(document).ready(function()
        {
            var page_count = {{count()}};
            var height= $(window).height();
            if (page_count == 4 )
            {
                $.ajax({
                    type: "GET",
                    url: "http://times.com/mobilepopuptracker?from=android", 
                });
                $('body').html('<div class="row flush aligncenter popbx" style="height:'+height+'px"><div class="12u">');               
            }
            else
            {
            }
    
        });
            function redirect()
            {
                  var a=$(location).attr('href');
                   window.location.href=a;
            }
        </script>
    

    问题

    • 我需要在视图源代码中隐藏 js 代码。

调试

  • 我已经参考了链接 查找解决方案 http://www.sitepoint.com/hide-jquery-source-code/。

  • 尽管代码仍被查看。

  • 欢迎任何建议。

  • 虽然我们知道我们不能停止在视图源代码中查看JS,但仍然必须有一些技巧。

使用在线 Google Closure 编译器服务,它会通过重命名变量和函数名称等操作使您的代码几乎不可读。例如:

原始 JS

function toggleDisplay(el){
    if (!el) return;
    el.style.display = (el.style.display==='none') ? 'block' : 'none';
}

闭包编译

function toggleDisplay(a){a&&(a.style.display="none"===a.style.display?"block":"none")};

JavaScript Beautified(美化

function toggleDisplay(a){
    a&&(a.style.display="none"===a.style.display?"block":"none")
};

这样做还可以减小脚本的大小,从而有助于增加网页的加载时间。

你仍然可以阅读脚本,但它更难理解,并且在使用 JavaScript 闭包之类的东西时会变得非常复杂。

你不能真正隐藏你的js代码。 你可以混淆它(即使其难以阅读),但与在服务器端处理的PHP或Perl不同,JS在客户端的浏览器中运行。 因此,客户端始终具有它的副本,并且可以随时查看该源。