Code from the previous page is executed again on new page #382

Closed
vschoettke opened this Issue Jul 31, 2012 · 1 comment

Comments

Projects
None yet
2 participants
@vschoettke

If a page is loaded within javascript, old code that was executed previously is executed again on the new page. This behaviour occurs on:

  • location.reload();
  • setting location.href
  • submitting a form with submit

In my example below the testFunction() is called twice after loading the same page even tough it should be called only once. see Output "Item already exist". The code is once called with "old" arguments that it previously ran and once with the current arguments, but my example does not demonstrate this.

During my tests this behaviour only happend if:

  • the code that is executed is in an external file
  • the code that causes the page to load is invoked via pressButton or clickLink (so all events I guess). Executing the JavaScript code that does the submit/page loading via evaluate() works fine.

Here is code to reproduce the problem:

index.html

<html>
<head>
    <script type="text/javascript" src="test.js"></script>
</head>
<body>
<form name="testForm" method="get" action="/index.html">
</form>
<a id="testlink" href="" onclick="document.forms['testForm'].submit();">Test</a>
<script language="JavaScript" type="text/javascript">
    testFunction();
</script>
</body>
</html>

test.js

function testFunction()  {
    console.log("testFunction");

    if (document.querySelectorAll('input[name="doubletest"]').length > 0) {
        console.log("Item already exist");
    }

    var newField = document.createElement('input');
    newField.setAttribute('type', 'hidden');
    newField.setAttribute('name', 'doubletest');
    document.forms[0].appendChild(newField);
}

doubleInputTest.js

/*jslint node: true */
'use strict';
var zombie = require("zombie");
var assert = require("assert");
var browser = new zombie.Browser({
    debug: true
});

function checkInputElemCount(browser) {
    var b = browser.querySelectorAll('input[name="doubletest"]');
    b.forEach(function(e) { console.log(e.outerHTML); });
    assert.equal(b.length, 1, "There should be only one input element");
}

browser.visit('http://127.0.0.1:9000/index.html').
    then(function () {
        console.log("Page loaded first time");
        checkInputElemCount(browser);
        return browser.clickLink('#testlink');
    }).
    then(function () {
        console.log("Page loaded 2nd time");
        checkInputElemCount(browser);
        // return browser.pressButton('#submitbutton');
        return browser.clickLink('#testlink');
    }).
    then(function () {
        console.log("Page loaded 3rd time");
        checkInputElemCount(browser);
    }).
    fail(function (error) {
        console.log("Error:", error);
    });

Start python -m SimpleHTTPServer 9000 in the same folder as index.html and test.js then start node doubleInputTest.js

I tested this with zombie 1.3.1 (which depends on jsdom 0.2.14)

@vschoettke

This comment has been minimized.

Show comment Hide comment
@vschoettke

vschoettke Aug 2, 2012

After debugging zombie.js I recognized that the reason for it to load the page again is that the script in the onclick does not prevent the default action (by returning false).

However the main problem still exists that zombie.js executes code that it shouldn't. After setting href or taking any other action that changes the URL any browser stops executing and starts "fresh" on the the new page after returning to the event loop.

After debugging zombie.js I recognized that the reason for it to load the page again is that the script in the onclick does not prevent the default action (by returning false).

However the main problem still exists that zombie.js executes code that it shouldn't. After setting href or taking any other action that changes the URL any browser stops executing and starts "fresh" on the the new page after returning to the event loop.

@assaf assaf closed this Sep 25, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment