1

I created a redirect function to redirect page when user submits input to the search box. When user search something, it will redirect to some webpage. Let's say that redirected url is "http://www.google.com" (In real, I used different URL to redirect, but just picked google.com for example) and below is what I created.

<script>
function redirect(){
    window.location = "http://www.google.com";
    return false;
}
</script>

The question is---- How can I be able to run another JavaScript <script>alert("hi")</script> when http://www.google.com webpage is successfully loaded?

I'm very new to JavaScript and HTML code, so any advise would be appreciated!

Hee Park
  • 11
  • 1
  • 1
    You cannot do this with Google, you lose control of the page when you redirect someone off of your own website. Is the real website you will be redirecting to one that you own and control or the same site? If so, then there are probably other approaches that work better. – ryanm Sep 22 '20 at 04:17
  • https://stackoverflow.com/a/2797574/7856807 – Kopi Bryant Sep 22 '20 at 04:36
  • Please check onload events https://www.w3schools.com/jsref/event_onload.asp and if you are using jQuery so jQuery(document).ready(function(){ redirect(); }); – WordPress Mechanic Sep 22 '20 at 05:10
  • @ryanm Hello! I am not trying to run this on Google! I am working on my school project and they gave us the URL to redirect. So, what I was trying to do was craft a html webpage from localhost that can redirect to the URL that was provided by school and run javascript code. – Hee Park Sep 22 '20 at 13:12
  • @KopiBryant Thank you I'll check that out – Hee Park Sep 22 '20 at 13:13
  • @WordPressMechanic Thank you! I'll check that out – Hee Park Sep 22 '20 at 13:13

1 Answers1

0

Unfortunately this is not possible. After redirecting to another page, you are not able to perform any actions on it, because you don't own this page and have no permissions to do so (except you are also the owner of the other page). This is similar to iFrames. You cannot access the foreign page, because then you could manipulate it and change it's content.

In your case the only action you could perform is, to show a dialog right before redirecting to another page.

JKD
  • 1,279
  • 1
  • 6
  • 26