How do I hide javascript code in a webpage?
I'm not sure anyone else actually addressed your question directly which is code being viewed from the browser's View Source command.
As other have said, there is no way to protect JavaScript intended to run in a browser from a determined viewer. If the browser can run it, then any determined person can view/run it also.
But, if you put your JavaScript in an external JavaScript file that is included with:
<script type="text/javascript" src="http://mydomain.example/xxxx.js"></script>
tags, then the JavaScript code won't be immediately visible with the View Source command - only the script tag itself will be visible that way. That doesn't mean that someone can't just load that external JavaScript file to see it, but you did ask how to keep it out of the browser's View Source command and this will do it.
If you wanted to really make it more work to view the source, you would do all of the following:
- Put it in an external .js file.
- Obfuscate the file so that most native variable names are replaced with short versions, so that all unneeded whitespace is removed, so it can't be read without further processing, etc...
- Dynamically include the .js file by programmatically adding script tags (like Google Analytics does). This will make it even more difficult to get to the source code from the View Source command as there will be no easy link to click on there.
- Put as much interesting logic that you want to protect on the server that you retrieve via AJAX calls rather than do local processing.
With all that said, I think you should focus on performance, reliability and making your app great. If you absolutely have to protect some algorithm, put it on the server, but other than that, compete on being the best at what you do, not by having secrets. That's ultimately how success works on the web anyway.
how to secure or hide my javascript code?
You can't. Don't put sensitive code, keys, information, or logic on the client. The only thing you can do is make it harder to read by running your code through an obfuscator.
How to hide html source code of website when user use view source option
Another silly option which allows you not to show the source code is by doing a Single Page Application (all modern Javascript framework like Angular, React or Vue are made in this scope).
In this case the source code will be an index.html
file nearly empty.
The html will be generated dynamically through you javascript code (by the use of template or JSX syntax)
PS: in this way you can still see the generated html in the console of the browser (like Elements tab in Chrome)
how to hide js value from inspection on web
The browser works for the user not the site author.
You cannot give something to the browser and expect it to keep it a secret from the user.
Do the check server-side. Live with the extra HTTP request.
How to hide javascript file of your website?
Javascript can be obfuscated, but there's nothing that's going to prevent a client from
- seeing the URL strings in your code, or
- simply inspecting the HTTP requests themselves to determine what URLs are being hit.
This re-enforces the importance of making sure you write solid and secure server-side code. You also want to make sure your web server is configured and secured properly, so that (for example) clients are unable to download the PHP source directly.
Hide JS Library
No, AFAIK there isn't, because the server cannot see whether the browser requests the JS file to execute it or to display its source code to the user. The only thing you can do is to minify your JS file; the browser will still execute it, but for the user it's hard to read it. But it's not hard for anyone to un-minify / tidy it.
EDIT: If you have access to the server, you can configure it to compress your JS file before sending it to the browser - The browser decompresses it for execution. I don't know though if the browser will decompress it too, when the file is viewed directly. If he would, it wouldn't make any sense to compress it (except for slightly faster load times).
Related Topics
Differencebetween String Primitives and String Objects in JavaScript
How Does JavaScript's Sort() Work
How to Send a Variable Number of Arguments to a JavaScript Function
How to Capture an Arbitrary Number of Groups in JavaScript Regexp
Why Does JavaScript Handle the Plus and Minus Operators Between Strings and Numbers Differently
Array.Fill(Array) Creates Copies by References Not by Value
How to Parse JSON Using Node.Js
Differencebetween Parseint() and Number()
How to Get the Scrollbar Position with JavaScript
JavaScript % (Modulo) Gives a Negative Result for Negative Numbers
Jquery How to Bind Onclick Event to Dynamically Added HTML Element
Get the Element with the Highest Occurrence in an Array
How to Add a Custom Right-Click Menu to a Webpage
How to Check for an Undefined or Null Variable in JavaScript
JavaScript Global Event Mechanism
Object Spread VS. Object.Assign