简体   繁体   中英

Why I can not get the updated "cars" array in my new popped up window

My index.html has a button( id="my-btn" ):

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>My INDEX PAGE</title>
    </head>
    <body>
        <br><input type="button" id="my-btn" value="OPEN NEW WINDOW"/>
        <script src="js/my.js"></script>
    </body>
    </html>

above page include my.js which handles button click event, when button clicked, a new browser window will be opened with a new page( test.html )

my.js:

$('#my-btn').click(function(){  
  window.open('test.html', 'testwindow');
});

The new page ( test.html ) opened/popped up in new browser window: test.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TEST</title>
</head>
<body>
    <div id="cars"></div>
    <script src="js/jquery-1.5.1.js"></script>
    <script src="js/mycar.js"></script>
    <script src="js/test.js"></script>

</body>
</html>

In the new page, test.js is included which will call functions in MyTest and append the text of number of cars as the content of this pop-up page:

js/test.js

$(document).ready(function(){
    MyTest.updateCars(); //MyTest is a function defined in mycar.js
    var cars = MyTest.getCars(); 
$("#cars").append("<strong>number of cars: "+cars.length+"</strong>");
});

Function MyTest is defined in mycar.js:

js/mycar.js

var MyTest = function(){
    var cars=[];

    var updateCars=function(){
        cars.push('car 1', 'car 2');
    };

    return{
        updateCars: function(){
            updateCars();
        },
        getCars: function(){
            console.log(cars.length);
            return cars;
        }
    };
}();

But, when the test.html page popped up in a new browswer window, the number of cars is always 0.

As you saw above I have called updateCars() function of mycar.js in test.js but the pop up window does not show the updated cars number , how can I get the updated cars(that's 2 cars, car1 and car2) in the new popped up window?

(I have jquery-1.5.1.js under "js/" directory)

hey i pasted your code in a page and it displayed

 number of cars: 2

it seems to work are you just referencing the script wrong? have you tried the debugger on chrome or firebug?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM