简体   繁体   English

如何获得在第一页生成的另一个php文件上创建的JavaScript对象?

[英]How do I obtain a JavaScript object that is created on another php file made by the first page?

My problem is I need to access a JavaScript object which is created on another page(php file) and using it in my current page. 我的问题是我需要访问在另一个页面(php文件)上创建并在当前页面中使用它的JavaScript对象。

So, this is my first page test_ong.php containing 2 buttons. 因此,这是我的第一页test_ong.php,其中包含2个按钮。 One is to create another page that produced that JavaScript object and another button to retrieve the JavaScript object. 一种是创建另一个生成该JavaScript对象的页面,另一个创建检索JavaScript对象的按钮。

<button id="new">Open New Win</button>
<button id="dis">Display</button>
<div id="test"></div>
<script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>

<script type="text/javascript"> 
var myChild;
$("#new").click(function() {
    myChild = window.open("test_reg.php","","width=500, height=500, resizable=yes");
});

$("#dis").click(function() {
    console.log('I clicked this');
    $.post("test_reg.php", function(data) {
        console.log(data);
    });
});
</script>

And my second page test_reg.php which will create the object whatIWant . 我的第二页test_reg.php将创建对象whatIWant

<script type="text/javascript">
    var whatIWant = {
        fruit: "apple",
        car: "Lotus",
        age: 31,
        hobby: "coding"
    };
</script>

So, what I want is whenever I click on button Display. 所以,我想要的就是每当我单击显示按钮。 It will give me the object whatIWant in the console log. 它将在控制台日志中为我提供对象whatIWant

I know I can get this by using myChild.whatIWant but I need to refresh my first page and still get the object myChild.whatIWant . 我知道我可以通过使用myChild.whatIWant来获取此信息,但是我需要刷新我的第一页并仍然获取对象myChild.whatIWant

Thank you for those who read or answer my question. 感谢您阅读或回答我的问题的人。

Your second page should return only the object like: 您的第二页应仅返回如下对象:

 {
        fruit: "apple",
        car: "Lotus",
        age: 31,
        hobby: "coding"
 }

You can do this by ajax call, you need to first initialize a variable, then through ajax call you can over write it and then display again. 您可以通过ajax调用来做到这一点, 首先需要初始化一个变量,然后通过ajax调用可以覆盖它 ,然后再次显示。

<!DOCTYPE html>
    <html>

      <head>
        <link rel="stylesheet" href="style.css">
        <script>var whatIWant = {};</script>
      </head>

 <body>
    <button id="new">Open New Win</button>
    <button id="dis">Display</button>
    <div id="test"></div>
    <script
      src="https://code.jquery.com/jquery-3.1.1.min.js"
      integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
      crossorigin="anonymous"></script>

    <script type="text/javascript"> 
    var myChild;
    $("#new").click(function() {
        myChild = window.open("test_reg.php","","width=500, height=500, resizable=yes");
    });

    $("#dis").click(function() {
        console.log('I clicked this');

        $.ajax({
                type: "POST",
                url: "test_reg.php",
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                   alert(data);
                },
                error: function (errormessage) {

                    //do something else

                }
            });
    });
    </script>
      </body>

    </html>

Here is your test_Reg.php file : 这是您的test_Reg.php文件:

echo '<script type="text/javascript">
      whatIWant = {fruit: "apple",
      car: "Lotus",
      age: 31,
      hobby: "coding"};
      </script>';

Maybe you can try storing the JSON as a session variable or in a cookie, and then retrieve it with Javascript. 也许您可以尝试将JSON作为会话变量或cookie存储,然后使用Javascript检索它。 Both should be accessible after page reload. 重新加载页面后,两者都应该可以访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中,如何获取gridview的当前页面索引? - In Javascript, how do I obtain a gridview's current page index? 我如何在网页上存储由javascript所做的更改? - How do I have a web page store changes made by javascript? 如何将PHP变量传输到另一个JavaScript文件? - How do I transfer the PHP variables to another javascript file? 如何将使用JavaScript创建的div传递给使用php的另一个页面 - How to pass a div created with javascript to another page with php 如何在php中附加到json文件并将其解析为javascript中的对象? - How do I append to a json file in php and parse it into an object in javascript? 如何获取使用 php/JavaScript 上传的文档第一页的屏幕截图 - how do I get a screenshot of the first page of a document uploaded using php/JavaScript 如何将(通过javascript)动态创建的HTML表的数据发送到PHP文件进行处理? - How do I send the data of an (by javascript) dynamically created HTML table to a PHP file to process? 如何使用Javascript获取URL变量 - How do I obtain URL variables with Javascript 如何通过Javascript将php会话从一页进行到另一页? - How do I carry over a php session from one page to another via Javascript? 在另一个 js 文件中进行 dom 操作后,如何运行 javascript 文件? - How can I run javascript file after dom manipulation made in another js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM