PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 24, 2022

[FIXED] How to debug JavaScript modules using the browser console?

 November 24, 2022     javascript, module     No comments   

Issue

I am using my JavaScript file as a module:

<script type="module" src="main.js"></script>

but, when my HTML is loaded, any object inside the main.js is not accessible from the browser debugging console, saying that the variable is not defined.

main.js:

var MyVar = 10;

enter image description here

When putting a breakpoint in main.js I can access the objects fine. But then the program is stopped, and I want to manipulate some while everything is running.

How can I access (global) objects in the JavaScript files I have included as module?


Solution

You can put the breakpoint in the module, then when the program is stopped, inspect the scope to find the object that you are looking for, then right-click and store as global variable. Even after continuing execution, the object will be available in the global scope under that variable. You can also do this manually by putting an assignment window.myGlobal = interestingObject either directly in the module code or enter it in the console while stopped at a breakpoint.



Answered By - Bergi
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing