迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在查看源代码中隐藏 JavaScript 代码

作者:迹忆客 最近更新:2024/03/22 浏览次数:

本教程介绍如何从 Web 浏览器的 查看源代码 功能中隐藏 JavaScript 代码。

这些步骤涉及将 JavaScript 代码放置在外部 JavaScript 文件中。之后,你可以将文件动态插入到当前页面中。


在查看源代码中隐藏 JavaScript 代码

  1. 编写 JavaScript 测试代码

    编写一个新的 JavaScript 代码并使用 .js 扩展名保存它。这个 JavaScript 文件将是我们将在 查看源代码 中隐藏的文件。

    接下来,你可以将以下 JavaScript 代码保存为 test-viewsource.js

    alert('My code is not visible in View Source');
    
  2. 准备一个 HTML 文件

    此 HTML 应包含你希望在网页上显示的内容。同时,它还将包含一个 <script> 标签。

    在这个 <script> 标记中编写一些 JavaScript 代码。将有几行代码将 test-viewsource.js 动态包含到 HTML 文件中。

    因此,你不会在 Web 浏览器的 View Source 中看到 test-viewsource.js

    你可以使用下一个 HTML 来跟进。将文件另存为 no-viewsource.html

    <body>
    	<main>
    		<p>The included JavaScript code is not visible in View Source</p>
    	</main>
    
    	<script type="text/javascript">
    		/`
    		 * Code to hide from view source will go here.
    		 * Check the next code block.
    		 */
    	</script>
    </body>
    
  3. <Script> 标签之间写一些 JavaScript 代码

    no-viewsource.html 文件的 <script> 标记内,我们将编写动态加载 test-viewsource.js 的代码。代码将遵循下面概述的步骤。

    1. 使用 document.createElement 方法创建一个新的 script 元素。
    2. 将脚本 type 属性设置为 text/javascript
    3. src 属性设置为 test-viewsource.js
    4. 将这个新的 script 元素附加到 HTML body 元素。

    下面的代码是这些步骤的实现。

    <script type="text/javascript">
    		let scriptElement = document.createElement("script");
    		scriptElement.type = "text/javascript";
    		scriptElement.src = "test-viewsource.js";
    
    		document.body.appendChild(scriptElement);
    </script>
    
  4. 测试代码

    在你的网络浏览器中加载 no-viewsource.html 并在其上使用查看源代码。如果你做的一切都正确,你将看不到 test-viewsource.js 的代码。

    相反,你会看到将其动态包含到 no-viewsource.html 中的代码。下图是你应该在 Web 浏览器的查看源代码中看到的示例输出示例。


结论

在这个阶段,test-viewsource.js 中的 JavaScript 代码不会出现在 Web 浏览器的 View Source 中。同时,任何人都可以在浏览器中写入你网站的 URL 以查看代码。

作为建议,不要在 test-viewsource.js 中放置任何敏感信息。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便