javascript : How to write inside popup window

标签 javascript

我创建了一个 javascript 程序,我创建了电影对象,我创建了一个名为 myWin 的新窗口,在 myWin 中我创建了另一个名为 actorWin 的窗口。现在,当我单击单击此处以访问 Actor 窗口时,我想在 actorWin 中传递 url(myMovie.actor) 和描述(myMovie.website_actor)。我试过下面的方法,但那是行不通的。谁能建议我如何在 actorWin 弹出窗口中编写。

代码:

<!DOCTYPE html>
<html>
<head>

    <title>lab 8</title>
    <script type="text/javascript">

        var myWin = window.open("", "myWin", "height=500, width=500,location,menubar,toolbar,status,resizable");

        function movie(movie_title, website_title, actor, website_actor){
            this.movie_title = movie_title;
            this.website_title = website_title;
            this.actor = actor;
            this.website_actor = website_actor;
        }

        var myMovie = new movie("Before she was Wonder Woman, she was Diana, princess of the Amazons, trained to be an unconquerable warrior. Raised on a sheltered island paradise, Diana meets an American pilot (Chris Pine) who tells her about the massive conflict that's raging in the outside world. Convinced that she can stop the threat, Diana leaves her home for the first time. Fighting alongside men in a war to end all wars, she finally discovers her full powers and true destiny.", 

            "http://www.imdb.com/title/tt0451279/",

            "Gal Gadot is an Israeli actress, singer, martial artist, and model. She was born in Rosh Ha'ayin, Israel, to an Ashkenazi Jewish family. Her parents are Irit, a teacher, and Michael, an engineer, who is a sixth-generation Israeli. She served in the IDF for two years, and won the Miss Israel title in 2004.", 

            "http://www.imdb.com/name/nm2933757/?ref_=tt_cl_t1");

        myWin.document.write(    
            "<script type='text/javascript'>"  
            +    "function movieWindow() {"

            +   "var movieWin = window.open(\"" + myMovie.website_title + "\" , \"movieWin\", \"height=500, width=500,location,menubar,toolbar,status,resizable\");"

            +   "}"

            + "function closeMovie() {"

            +  "movieWin.close()"

            +   "}"

            + "<\/script>");

        myWin.document.write(
        "<script type='text/javascript'>" 
          + "function actorWindow() {"

          +   "var actorWin = window.open(\"''\" , \"actorWin\", \"height=500, width=500,location,menubar,toolbar,status,resizable\");"

          +   "<p style='color: green; font-size: 150%'>  \""+ myMovie.actor + "\" </p>"
          +   "<a  style='color: pink; font-size: 150%' href= \""+myMovie.website_actor +"\"> Click for more info </a> "

          +   "}"

           +    "<\/script>");



        myWin.document.write(

            +"<script type='text/javascript'>"

            +   "<body style='background-image : url(lab8_images/back.png)'>"
            +   "<h1 style= 'text-align: center; color: white; font-family: monospace; font-size: 200%'> What about this movie? </h1>"
            +    '<br/>' + '<br/>' +'<br/>' 

            + "<p style = 'font-family: monospace; font-size: 150%; color: #ffffff; padding: 0px 15px: 0px 15px; text-decoration: none'> \""+ myMovie.movie_title +"\"  </p>"


            +  "<p style = 'font-family: monospace; font-size: 150%; color: #ffffff; text-align:center; text-decoration: none'> <a style='color:white' href = 'javascript: movieWindow()' > CLICK HERE TO ACCESS TO THE MOVIE WINDOW </a><br></p>"

            +   "<p style = 'font-family: monospace;font-size: 150%; color: #ffffff;  text-align: center; text-decoration: none'> <a style='color:white' href = 'javascript: actorWindow()'> CLICK HERE TO ACCESS TO THE ACTOR WINDOW </a><br></p>"

            +   "<p style = 'font-family: monospace; color: white; text-align: center; text-decoration: none; font-size: 150%'> <a style='color:white' href = 'javascript: closeMovie();'> CLICK HERE TO CLOSE THE MOVIE WINDOW </a><br></p>"

            +   "<p style = 'font-family: monospace; color: white; text-align: center; text-decoration: none; font-size: 150%'><a style='color:white' href='javascript:actorWindow.close();'> CLICK HERE TO CLOSE THE ACTOR WINDOW </a><br></p>"

            +   "<p style = 'font-family: monospace; color: white; text-align: center; text-decoration: none; font-size: 150%'><a style='color:white' href='javascript:window.close();'> CLICK HERE TO CLOSE THIS WINDOW </a><br></p>"
            +"  <\/script>"
            );

        </script>

    </head>
    <body background = lab8_images/back.png>

    </body>
    </html>

输出:

/image/xbPIL.jpg

最佳答案

您可以使用 window.openerpostMessage() 在浏览上下文之间进行通信

初始 HTML

<script>
  let outerWindow = window.open("myWin.html", "_blank");
  window.onmessage = function(e) {
    console.log(e.data);
    // pass data to inner window
    e.source.postMessage(JSON.stringify({a:1, b:2}), document.origin); 
  }
<script>

在“actorWin”HTML

<script>
  window.onmessage = function(e) {
    console.log(e.data); // message from `window` at initial HTML `document`        
  }
  window.opener.opener.postMessage("inner message", document.origin);
</script>

plnkr http://plnkr.co/edit/99cHuMklH9S9d4Rgf73K?p=info

关于javascript : How to write inside popup window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519897/

相关文章:

javascript - 基于 "screen width"的jQuery条件

javascript - 使用 setInterval() 的 jQuery 动画

javascript - 如何使用 Jint 从 JavaScript 函数调用中获取正确的 C# 字符串数组

javascript - 类型错误 : Cannot read property 'then' of undefined on webservice

javascript - promise 被拒绝或履行困惑

javascript - 选择类(class)的第一个 child 在 <select> 中不起作用

JavaScript Accordion 效果不起作用,与伪类有关?

javascript - Protractor 中的 `browser.call()` 代表什么?

javascript - 无法使用 Protractor 运行 cucumber 测试

javascript - 从 Angular 资源获取 promise