Skip to content
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

Accessing global variables in separate scripts #874

Open
kincsescsaba opened this issue Sep 24, 2024 · 5 comments
Open

Accessing global variables in separate scripts #874

kincsescsaba opened this issue Sep 24, 2024 · 5 comments

Comments

@kincsescsaba
Copy link

I have an HTML loading multiple scripts:

<html>
  <head>
    <script src="scriptDeclaringAGlobalVar.js"></script>
    <script src="scriptReadingAGlobalVar.js"></script>
...

scriptDeclaringAGlobalVar.js:

var aGlobalVar = "hello"

scriptReadingAGlobalVar.js:

$(document).ready(function(){
  console.log(aGlobalVar);
});

Though the global variable is declared in the first script, and even $(document).ready was used, it can't be reached, I get errors like this in HtmlUnit:

jQuery.Deferred exception: "aGlobalVar" is not defined. @http://localhost:8080/scriptReadingAGlolbalVar.js:2
@https://code.jquery.com/jquery-3.7.0.js:3479
@https://code.jquery.com/jquery-3.7.0.js:3547

The same works fine in browsers.

@rbri
Copy link
Member

rbri commented Sep 25, 2024

Will have a look and maybe add a test case but if fear this works in general and is related to the jQuery initialization

@kincsescsaba
Copy link
Author

The above example is simplified. I checked putting a $(document).ready block also in scriptDeclaringAGlobalVar.js, which successfully read aGlobalVar. That's why I think the issue is related to handling scopes of multiple script includes.

@rbri
Copy link
Member

rbri commented Nov 11, 2024

@kincsescsaba Have just added a small test implementation to the test suite.

Can you please have a look at the test and check if i got your problem right.

@rbri
Copy link
Member

rbri commented Nov 19, 2024

@kincsescsaba still there?

@kincsescsaba
Copy link
Author

@rbri Sorry for the delay; I'm here now and just started to check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants