Home > Enterprise >  Remove last 3 letters of div (hidde also in browser page source)
Remove last 3 letters of div (hidde also in browser page source)

Time:08-05

this is my HTML

<div id="remove">Username</div>

and this is my JS code

function slice() {
var t = document.getElementById("remove");
t.textContent = t.textContent.slice(0, -3);
}
slice();

Username load from foreach

{foreach from=$last_user item=s}
{$s.date}
{$s.username}
{/foreach}

This code working and remove 3 letter but when right click on browser and look at page sources i can see "Username" ! I need remove three letter because of privacy and security . something like
*** name or usern ***

Thank for help me !

CodePudding user response:

The only secure way to make sure the client can't see a particular piece of information is to never send it to the client in the first place. Otherwise, there will always be a way for the client to examine the raw payloads of the network requests and figure out the information they aren't supposed to know.

You'll need to fix this on your backend - either hard-code in

<div id="remove">Usern</div>

or, for a more dynamic approach, use a template engine (or whatever's generating the HTML) and look up how to change strings with it. For example, in EJS, if user is an object with a username property, you could do

<div id="remove"><%= user.username.slice(0, -3) %></div>

Changing the content only with client-side JavaScript will not be sufficient, if you wish to keep some things truly private.

With Smarty, you can define a modifier that takes a string and returns all but the last three characters of it.

function smarty_modifier_truncate_three($string)
{
    return substr($string, 0, -3);
}

and then in your template, replace

{$s.username}

with

{$s.username|truncate_three}

If you want only the first three characters, it's easier because you can use the built-in truncate.

{$s.username|truncate:3}

CodePudding user response:

JS doesn't change the source, it can only change the DOM, so what you can do is to keep the element empty and add a value to it using js, but don't forget that js runs on the client's side so its better here to send the string from the server without the last 3 characters.

  • Related