-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsdom.jsdom() doesn't process script elements (even with the correct features) #426
Comments
There are a few issues with your sample code:
Here's the code with those changes: var jsdom = require('jsdom');
jsdom.defaultDocumentFeatures = {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script'],
MutationEvents : '2.0',
QuerySelector : false
};
var htmlDoc = '<html lang="en-US">' +
'head>' +
'<title>Test document</title>' +
'<script>' +
'var testVar = true;' +
'</script>' +
'<script src=\'http://code.jquery.com/jquery-latest.js\'></script>' +
'<script>' +
'</script>' +
'</head>' +
'<body id="mainPage">' +
'</body>' +
'</html>';
var document = jsdom.jsdom(htmlDoc);
var window = document.createWindow();
var elementsArray = window.document.getElementsByTagName('script');
console.log(elementsArray.length);
console.log(window.testVar);
window.addEventListener('load', function () {
console.log(typeof window.$ == 'function');
window.close();
}); |
Thank you for your comments. In regards to your first point:
Why is this required? Can't external resources be processed without mutation events? By the way, I read the warning on W3 website which seems to state that it is deprecated in the Level 3 Event specification. Anyhow, I believe that is outside the scope of the issue I reported :). The docs (i.e. readme) don't say that MutationEvents must be '2.0' for scripts to execute. Thanks for pointing out that globals go in the window object and not the document. I didn't understand this and should probably attempt to better understand the browser model and its scoping. As well, thank you for recommending a way to listen for the load event. |
JSDOM uses the mutation events internally to detect when script nodes are inserted into the document (see here). I think the iframe and css code use them, too. It would be better to use an internal mechanism, and I think someone did a little work on that, but not sure where it ended up.
It is mentioned in the MutationEvents section of the docs, but it probably isn't as clear as it should be ("Note:
Glad to help! |
Is window.testVar defined? |
Yes, it is defined under a script element in the code samples shown above. |
oh wow, sorry about that. I didn't get notification emails for Brian's posts. @brianmcd maybe another approach would work.. if |
That seems reasonable, possibly coupled with a warning message. For reference, @fgnass mentioned not using MutationEvents for this stuff in #295. I also wonder how big the performance gain is with these disabled, since it looks like the option was added for performance reasons (#128). If it's a small gain, it might be best just to remove the MutationEvents option. |
when Dynamicly creating a <script> tag , why it does not work? the script src will never loaded |
the following code , script node can't trigger onload event
|
Closed in favor of #500. |
I have been unable to get jsdom to execute script elements within an HTML page. Below is some example code:
I would expect to be able to have access to the testVar variable via the document. But if you execute the above the value of window.document.testVar will be undefined. I am using jsdom.jsdom because I need the flexibility of fetching and executing external references. As an example I did include a script element which points to the latest jQuery library. As far as I can tell I am using the library exactly as documented. Hence why I suspect an issue. But it is indeed possible that I am doing something wrong.
The text was updated successfully, but these errors were encountered: