我创建了一个小型机器人,我打算在列表中列出的所有内容上循环,并从另一页上的数据中获取数据。所以我只使用iframe,因此我可以覆盖并从中获取数据。
我的脚本问题是它在我的console.log中返回重复记录。它应该做的是从另一个页面获取数据并将其存储在父页面上。现在我只使用console.log
来查看它返回的内容。然后我可能会将它们存储在<textarea>
上,这样我就可以轻松地将其复制或存储在文件中。
<html>
<head>
<title>While Looping</title>
<script type="text/javascript">
var list = [ 644, 464, 441, 442, 552, 444, 594, 487, 430, 545, 524, 500, 450, 626, 505, 466, 456, 418, 560, 507, 535, 417, 462, 424, 551, 590, 453, 595, 593, 423, 495, 484, 536, 421, 518, 563, 515, 556, 532, 488, 465, 600, 597, 498, 534, 513, 514, 530, 519, 443, 596, 631, 533, 422, 437, 576, 504, 502, 361, 448, 598, 544, 416, 617, 537, 477, 447, 539, 614, 557, 445, 630, 588, 632, 476, 439, 574, 523, 636, 461, 558, 359, 499, 440, 481, 643, 460, 470, 569, 425, 573, 620, 615, 475, 468, 543, 599, 612, 613, 452, 582, 618, 455, 525, 438, 471, 628, 511, 562, 564, 565, 566, 605, 467, 463, 529, 561, 436, 527, 547, 623, 606, 486, 482, 555, 446, 559, 548, 549, 550, 607, 405, 540, 469, 474, 629, 611, 426, 639, 493, 602, 349, 604, 622, 546, 492, 494, 567, 407, 420, 399, 496, 531, 491, 520, 521, 404, 586, 459, 479, 637, 570, 634, 608, 584, 638, 601, 508, 516, 480, 571, 610, 621, 512, 483, 587, 592, 627, 642, 589, 428, 403, 541, 542, 625, 517, 522, 432, 497, 526, 644, 464, 441, 442, 552, 444, 594, 487, 430, 545, 524, 500, 450, 626, 505, 466, 456, 418, 560, 507, 535, 417, 462, 424, 551, 590, 453, 595, 593, 423, 495, 484, 536, 421, 518, 563, 515, 556, 532, 488, 465, 600, 597, 498, 534, 513, 514, 530, 519, 443, 596, 631, 533, 422, 437, 576, 504, 502, 361, 448, 598, 544, 416, 617, 537, 477, 447, 539, 614, 557, 445, 630, 588, 632, 476, 439, 574, 523, 636, 461, 558, 359, 499, 440, 481, 643, 460, 470, 569, 425, 573, 620, 615, 475, 468, 543, 599, 612, 613, 452, 582, 618, 455, 525, 438, 471, 628, 511, 562, 564, 565, 566, 605, 467, 463, 529, 561, 436, 527, 547, 623, 606, 486, 482, 555, 446, 559, 548, 549, 550, 607, 405, 540, 469, 474, 629, 611, 426, 639, 493, 602, 349, 604, 622, 546, 492, 494, 567, 407, 420, 399, 496, 531, 491, 520, 521, 404, 586, 459, 479, 637, 570, 634, 608, 584, 638, 601, 508, 516, 480, 571, 610, 621, 512, 483, 587, 592, 627, 642, 589, 428, 403, 541, 542, 625, 517, 522, 432, 497, 526, 616, 575, 585, 509, 554, 506, 454, 501, 431, 434, 603, 640, 641, 489, 451, 472, 510, 490, 473, 458, 457, 609, 568, 478, 619, 553, 538, 427, 635, 624, 583, 591, 572, 633, 429, 419, 503, 485, 435, 433, 528, 449, 340, 616, 575, 585, 509, 554, 506, 454, 501, 431, 434, 603, 640, 641, 489, 451, 472, 510, 490, 473, 458, 457, 609, 568, 478, 619, 553, 538, 427, 635, 624, 583, 591, 572, 633, 429, 419, 503, 485, 435, 433, 528, 449, 340 ];
var count = 0;
var total_list = list.length;
var page_id;
var base_url = "http://somewebsite.com/iframe_path";
var iframe_url = base_url + page_id;
function load_data() {
$("#iframe").load(function() {
var acc_name = $("#iframe").contents().find("#text_name").attr("value");
var acc_add = $("#iframe").contents().find("#text'_address").attr("value");
setTimeout(nextFrame, 1000);
console.log( list[count] + ' ' + acc_name + ' ' + acc_add );
});
$("h1").text(count);
} //load data
// loop records
function nextFrame() {
if(count != total_list) {
var iframe_url = base_url+list[count];
$("#iframe").attr('src',iframe_url);
console.log(iframe_url);
load_data();
count++;
//Continue the loop in 3s
//setTimeout(nextFrame, 1000);
}
}
// Start the loop
setTimeout(nextFrame, 0);
</script>
</head>
<body>
<h1>0</h1> <br />
<iframe width="400" height="400" id="iframe"></iframe>
</body>
</html>
答案 0 :(得分:1)
为什么要为iframe重新绑定“onload”?每次调用load_data()时,它会将另一个事件绑定到iframe,以便在加载时运行指定的函数。尝试这样的事情:
var list = [ 644, 464, 441, 442 ];
var count = 0;
var total_list = list.length;
var page_id;
var base_url = "http://somewebsite.com/iframe_path";
var iframe_url = base_url + page_id;
$(document).ready(function () {
$("#iframe").load(function() {
var acc_name = $("#iframe").contents().find("#text_name").attr("value");
var acc_add = $("#iframe").contents().find("#text'_address").attr("value");
console.log( list[count] + ' ' + acc_name + ' ' + acc_add );
setTimeout(nextFrame, 1000);
});
});
// loop records
function nextFrame() {
if(count != total_list) {
var iframe_url = base_url+list[count];
$("#iframe").attr('src',iframe_url);
console.log(iframe_url);
count++;
}
}