admin管理员组文章数量:1431399
I'm trying to use Chrome's dev tools to debug the flow of javascript on a page. I have a 3rd party system that loads javascript that dynamically adds a SCRIPT tag to the HEAD element on the page using some code like:
var head= document.getElementsByTagName("head")[0];
var el = document.createElement('script');
el.type = 'text/javascript';
el.src = '.js';
head.appendChild(el);
How can I catch the "anotherjsscript.js" as it is dynamically loaded and debug into that?
I'm trying to use Chrome's dev tools to debug the flow of javascript on a page. I have a 3rd party system that loads javascript that dynamically adds a SCRIPT tag to the HEAD element on the page using some code like:
var head= document.getElementsByTagName("head")[0];
var el = document.createElement('script');
el.type = 'text/javascript';
el.src = 'https://address./anotherjsscript.js';
head.appendChild(el);
How can I catch the "anotherjsscript.js" as it is dynamically loaded and debug into that?
Share Improve this question asked Feb 12, 2016 at 11:11 Dave PottsDave Potts 1,6432 gold badges24 silver badges38 bronze badges2 Answers
Reset to default 5Although BenG's answer will cover most cases, my case was badly obfuscated with a different filename dynamically served each time. My solution was to add an event listener for "Script first statement" and then laboriously work through all the other scripts until I hit the one I wanted.
Option 1
I just used your code above pointing to jQuery Validation:-
var head= document.getElementsByTagName("head")[0];
var el = document.createElement('script');
el.type = 'text/javascript';
el.src = 'https://cdnjs.cloudflare./ajax/libs/jquery-validate/1.14.0/jquery.validate.js';
head.appendChild(el);
After letting the page run for the 1st time, you will see the request under the Network Tab, right click on the script and select Open in source panel :-
then add your break point and refresh the page again:-
Which should then get hit.
Option 2
Another option is to create a function like below, which gets the script content, prepend a debugger
and set the scripts text to the content:-
function debugScript(url){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
var script = 'debugger; ' + xmlHttp.responseText;
var head = document.getElementsByTagName("head")[0];
var el = document.createElement('script');
el.type = 'text/javascript';
el.text = script;
head.appendChild(el);
}
debugScript('https://cdnjs.cloudflare./ajax/libs/jquery-validate/1.14.0/jquery.validate.js');
The breakpoint will get hit as long as the developer tools is open.
本文标签:
版权声明:本文标题:How to use Chrome dev tools to debug javascript tag added dynamically to a page's head element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584070a2664787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论