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
echo '
<script>
localStorage.clear();
localStorage.setItem("userPhotos", JSON.stringify(' . $photos . '));
</script>';
echo '
<script>
localStorage.clear();
localStorage.setItem("userPhotos", JSON.stringify(' . $photos . '));
</script>';
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
Jochem
Jochem•4mo ago
you can delete elements from the DOM, and that should include script elements, using .removeChild()
Blackwolf
BlackwolfOP•4mo ago
I need the data in the DOM, just not the JS I would've thought loading a new page would overwrite the first
Jochem
Jochem•4mo ago
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
Blackwolf
BlackwolfOP•4mo ago
but it's retaining the JS from the previous page
Jochem
Jochem•4mo ago
then it's not loading a new page how are you loading this page?
Blackwolf
BlackwolfOP•4mo ago
using MVC, showing the first page, followed by the second
Jochem
Jochem•4mo ago
is it a POST or GET request?
Blackwolf
BlackwolfOP•4mo ago
neither
Jochem
Jochem•4mo ago
so what are you using to show the new page then? MVC isn't a browser mechanism
Blackwolf
BlackwolfOP•4mo ago
i'm literally loading 2 seperate php pages one after the other
Jochem
Jochem•4mo ago
how though
Blackwolf
BlackwolfOP•4mo ago
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
Jochem
Jochem•4mo ago
like... with fetch? setting window.location? someone clicking a link?
Blackwolf
BlackwolfOP•4mo ago
just clicking a href link, which redirects to the controller, which then loads the relevant page
Jochem
Jochem•4mo ago
the controller is in PHP?
Blackwolf
BlackwolfOP•4mo ago
yea
Jochem
Jochem•4mo ago
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 DOM
Blackwolf
BlackwolfOP•4mo ago
page 2 doesn't have it's own link page 1 is like a "pre" page, just to send data to localstorage
Jochem
Jochem•4mo ago
so how does the browser go from page1 to page2?
Blackwolf
BlackwolfOP•4mo ago
the controller loads page1, then immediately loads page2
Jochem
Jochem•4mo ago
so the one request from the browser loads both pages in a single request
Blackwolf
BlackwolfOP•4mo ago
essentially
Jochem
Jochem•4mo ago
okay, so that's not a new DOM then
Blackwolf
BlackwolfOP•4mo ago
no, but it is a new page on the browser
Jochem
Jochem•4mo ago
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
Blackwolf
BlackwolfOP•4mo ago
so how do i clear the JS from the first page before loading the second?
Jochem
Jochem•4mo ago
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
Blackwolf
BlackwolfOP•4mo ago
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 🙂
Jochem
Jochem•4mo ago
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
Blackwolf
BlackwolfOP•4mo ago
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
Jochem
Jochem•4mo ago
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 request
Blackwolf
BlackwolfOP•4mo ago
i know, it's just my OCD, when there are 20 images and all associated data, it looks really untidy
Jochem
Jochem•4mo ago
that can also be an indication that your chosen approach isn't the most suitable
Blackwolf
BlackwolfOP•4mo ago
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
Jochem
Jochem•4mo ago
^ 20 images isn't session data, that's just data
13eck
13eck•4mo ago
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…
Blackwolf
BlackwolfOP•4mo ago
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 😄
Jochem
Jochem•4mo ago
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 table
13eck
13eck•4mo ago
But again, why? Why not just use an image tag? What makes your images so special they need special handling?
Ray
Ray•4mo ago
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.
Jochem
Jochem•4mo ago
I think Beck is suggesting they just use php to output the image tags rather than using Javascript after the page loads
Ray
Ray•4mo ago
Oh that makes more sense, and yeah i agree
13eck
13eck•4mo ago
Yeah, this. If you’re using PHP then use PHP
Jochem
Jochem•4mo ago
agreed

Did you find this page helpful?