How do I hide my (old) JavaScript from "View Page Source"?
I am passing info from PHP to local storage via one page, by basically writing JavaScript in PHP
THEN when loading the second page, the original JavaScript is still visible when you "View Page Source"
How do I make this disappear?
It's not because it's sensitive data, it just looks untidy when there is a lot, and that messes with my OCD lol
44 Replies
you can delete elements from the DOM, and that should include script elements, using
.removeChild()
I need the data in the DOM, just not the JS
I would've thought loading a new page would overwrite the first
then I'm not sure what you're asking
loading a new page does completely clear and rebuild the DOM from scratch based on the newly sent data
but it's retaining the JS from the previous page
then it's not loading a new page
how are you loading this page?
using MVC, showing the first page, followed by the second
is it a POST or GET request?
neither
so what are you using to show the new page then?
MVC isn't a browser mechanism
i'm literally loading 2 seperate php pages
one after the other
how though
intially i had the Controller load the JS, which kept it on the page, so thought adding it to its own page would rectify this
like... with fetch? setting window.location? someone clicking a link?
just clicking a href link, which redirects to the controller, which then loads the relevant page
the controller is in PHP?
yea
then the controller must be sending the original javascript along as well, cause a page load from clicking an
<a href="page2">
link will fully clear the DOM and replace it with a new DOMpage 2 doesn't have it's own link
page 1 is like a "pre" page, just to send data to localstorage
so how does the browser go from page1 to page2?
the controller loads page1, then immediately loads page2
so the one request from the browser loads both pages in a single request
essentially
okay, so that's not a new DOM then
no, but it is a new page on the browser
not if it's sent in the same request
cause the browser doesn't give a fuck about the controller, it will load whatever is sent to it from one request as a single page
so how do i clear the JS from the first page before loading the second?
delete it with removeChild
or if you want, load page1 in one request, and after the JS there has run, have it redirect the browser to page2 with window.location
but also? It's entirely pointless to remove that JS. No one will look at your source code
i do, and i hate it looking untidy. you should see how meticulously organised it all is lol
i'll look into removeChild - thank you sir 🙂
it does honestly sound a bit of a cursed way to load data though
just hardcoding it into a script tag?
if you have to load data, generally you'd set up an API endpoint, then load the data with a fetch request
i just need session data putting into JS
all my data is accessed via php and kept secure
only snippets get moved to JS
in fact only this photo editting thing has data in JS
alright... well, just keep in mind that any complications you add to your site is more cross section for bugs to appear in. If it's unnecessary except for
-_-*code aesthetics*-_-
, it's not going through any professional pull requesti know, it's just my OCD, when there are 20 images and all associated data, it looks really untidy
that can also be an indication that your chosen approach isn't the most suitable
is there another way to add session data to localstorage?
gtg, gone midnight and i'm up at 6am - will look back tomorrow, thanks for your input
^
20 images isn't session data, that's just data
Why are you trying to load 20 images via session data? Why not use an img tag? Or an API call?
This feels like an X/Y problem going on here…
the images are all on the same account, stored in one variable (serialised). essentially $account['photos'] is an array of photos and tags / flags
however, i have taken your advice and decided to leave the info in the "View Page Source", and just moved it all to the bottom out of the way 😄
I would argue that that's not the right way to store 20 photos
one or two, maybe, but once you hit that kind of amount it's time to pull out the
image
table and use a foreign key back to the user
tableBut again, why? Why not just use an image tag? What makes your images so special they need special handling?
I think they are using image tags to actually display the images; it sounds like they're using the browser's local storage as a database, which, uh. Yeah i would also argue that's the wrong way to store that.
I think Beck is suggesting they just use php to output the image tags rather than using Javascript after the page loads
Oh that makes more sense, and yeah i agree
Yeah, this. If you’re using PHP then use PHP
agreed