0

I'm using tampermonkey and doing some client side scripting and I'm wondering how I could apply and use the existing functions/objects and apply them when opening a new window with window.open (The page is in the same domain)

In this case I would need to change the values in the child window.

I am able to successfully get an alert on the new window that opens but I wouldn't know how to transfer a value from the parent window to the child once the page has loaded or with some delay.

var exampleParentWindowObject1 = 'example';

    exampleParentWindowfunction();  {
    CODE
    }





    var NewWindow =   window.open('https://webpage.com',"MsgWindow", "width=2000,height=1000");


              a.focus();

    NewWindow.addEventListener('load', function(){
    NewWindow.alert('test');
    }, 1000);
    });
        }
  • pass the values on the querystring and get the script on the child page to read them – ADyson Oct 10 '16 at 21:17
  • secondly, if you want to also share function code, as you mentioned, put the code in a separate .js file and have both pages reference them. – ADyson Oct 10 '16 at 21:18
  • "pass the values on the querystring and get the script on the child page to read them" How would I do that? Could you show an example? Should I transfer the variable within a function and Somehow replace the existing value in new window? How would I do that? – Alexius Oct 16 '16 at 15:46
  • e.g. how do i run a replace function on NewWindow? NewWindow.Vaue1.replace("givenvalue"); – Alexius Oct 16 '16 at 16:02
  • the querystring is in the URL. e.g. `htttp://www.example.com/mypage.html?val1=A&val2=B&val3=C` where "?" marks the beginning of the querystring, "val1" is a parameter name, "A" is the value of the "val1" param, and "&" marks the boundary between each parameter. when you use `window.open` and set the URL for the new window, you can include any parameters you want by appending them to the end of the URL using that format. Then, see this tutorial on how to extract the values in your second page, using Javascript: https://davidwalsh.name/query-string-javascript – ADyson Oct 17 '16 at 08:40
  • in answer to your second question, you can't use script from one page to control script from another page - think of the security risks if any two webpages that you had open at the same time could manipulate each other! The pages are independent (and stateless) so you need to think more in terms of moving from one state to another and passing messages/objects between the screens, not of multiple screens/forms directly talking to each other (like they might in a desktop app) – ADyson Oct 17 '16 at 08:43
  • the only other (very new) solution you could consider, if both pages are on the same domain, is Local Storage. http://stackoverflow.com/questions/19231558/change-element-attribute-of-another-page – ADyson Oct 17 '16 at 08:44

0 Answers0