Inject an opened window with script

前端 未结 3 1203
我在风中等你
我在风中等你 2020-12-14 23:30

This question asks for a way to open a new window using window.open and then inject it with a script. It was not possible because of cross-domain security issue

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 23:54

    Here's a trick I use, it uses query strings, and is client side. Not perfect but it works:

    On the sending page, do:

    var javascriptToSend = encodeURIComponent("alert('Hi!');");
    window.open('mypage.html?javascript=' + javascriptToSend);
    

    Replace mypage.html with your page. Now on the receiving page, add:

    (location.href.match(/(?:javascript)=([^&]+)/)[1])&&eval(decodeURIComponent(location.href.match(/(?:javascript)=([^&]+)/)[1]));
    

    You'll have to do some back-and forth to make sure this works.


    If you HAVE PHP you can use this more reliable solution on the receiving page:

    eval(decodeURIComponent());
    

提交回复
热议问题