Skip to content

Accessing global variables in separate scripts #874

Description

@kincsescsaba

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.

No activity

Activity on this issue will appear here.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions