Get line number of Chrome inspected element - google-chrome

I'm using the Chrome browser's Inspect Element function. It opens some kind of tab in my browser, shows me the HTML, and lets me change it and do some kind of preview. When I change it, I want to know where can I see the line number in my source file. Inspect Element highlights the line of the element that I pick, but when I go to the Source tab, there's no highlighted line.

There's a problem with that: the DOM tree that you see in "inspect element" may not be the same as defined in the HTML file.
JavaScript can modify the DOM tree in arbitrary ways. Afterwards, it's impossible to match a certain node to the original source.
So, your task is nearly impossible and that is why Dev Tools do not try to do it.

Related

Finding source of second call to own page in devtools

Debugging a page where I see that there are two calls made to the page, one is the initial call and one is made when the first has been recieved and the browser parses the document. I am however having trouble finding the source of the second call. I have built the javascript i dev mode so I have sourcemaps but the call seems to be originating from the html code itself. Looking in the Network tab of devtools I see this
in Chrome and this
in Firefox
Firefox seems to identify the Initiator as image but no more details as to what image.
What should I look for in the code to find the source of the call? Is there something else I can do to get more details of where the call is made from?
Update:
Clicking the initator item in Chrome marks the first Doctype line in the document
That the request is recognized as an image is an indication for either an <img> HTML element or a url() CSS function that has a URL that references the document itself instead of an image.
Check the HTML
From my tests, this does not happen when the src attribute is empty but it happens when you set it to an anchor on the page.
So, in order to find the culprit, go to the Inspector (Firefox) or Elements (Chrome) panel, press Ctrl+F (Windows, Linux) or Cmd+F (macOS) and search for img[src^="#"]. That finds all <img> elements that have a src attribute with a value starting with a hash.
Check the CSS
Browsers (incorrectly) send a network request when the CSS url() function is set to an empty string, i.e. url(), url(''), or url("").
To check whether it is set within the style attribute of one of the HTML elements, search for url('') and url() within the Inspector as described above.
If it cannot be found there, you need to search within the style sheets.
Chrome
In Chrome this is possible by pressing Ctrl+Shift+F (Windows, Linux) or Cmd+Shift+F (macOS) while the focus is within the DevTools. Doing so opens a search tool that allows to search across all loaded sources. Type url('') in there and hit Enter. If nothing is found, try it again with url("") and url().
Firefox
In Firefox there's no such global search yet (as of Firefox 85), unfortunately. Though there are two ways other ways to search through the style sheets.
Via the Style Editor
The Style Editor allows you to inspect the style sheets, though you have to search in each one separately by selecting it at the left side and then pressing Ctrl+F (Windows, Linux) or Cmd+F (macOS), typing url(), url(''), or url("") and hitting Enter.
Via the Network Monitor
There's a feature that allows you to search through the network request reponses within the Network Monitor. Click the magnifying glass button, which opens the search tool. In the input field type url(), url(''), or url("") and hit Enter.

Why would links in an object or iframe (I've tried both) start opening in a new window

I'm working on an application that has many links. They all open in the same window, until today. All of a sudden, in all browsers I'm testing in, 3 links in an iframe or object (I've tried both) start opening in a new window. I can't seem to stop this.
An object example follows. The dolnks program generates 3 simple links like the one following the object example and these links open in a new window.
<OBJECT ID='fixed' DATA='dolnks.cgi?str=$params' TARGET='dynamic' NORESIZE></OBJECT>
darea.cgi?str=$dogstr
Can someone help me understand this and how to get these links to open in the same window.
All links that now open in the same window or should be opening in the same window are from
the same domain.
I now have to close the link instead of using the back button.
Thanks,
craigt
I imagine it's because of your TARGET attribute, although it's not using one of the special target values:
target
Where to display the linked URL, as the name for a browsing context (a tab, window, or ). The following keywords have special meanings for where to load the URL:
_self: the current browsing context. (Default)
_blank: usually a new tab, but users can configure browsers to open a new window instead.
_parent: the parent browsing context of the current one. If no parent, behaves as _self.
_top: the topmost browsing context (the "highest" context that’s an ancestor of the current one). If no ancestors, behaves as _self.
From the MDN Docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes
Note though that the docs for object do not list a target attribute, so that behavior is apparently undefined, and probably varies depending on the browser, and the plugin displaying the object.
Check to see if it's the items with a target attribute (case does not matter) that are working "wrong", and see if removing that attribute fixes it. If that's not it, next check to see if there are javascript being loaded. Try turning javascript off (hopefully the relevant links are not generated with javascript) and see if that fixes the behavior. If turning javascript off is too heavy handed, you can use the javascript console to see what listeners are attached to the links.

How can I use Chrome Devtools to figure out what adds a certain element to the page?

This question is similar to How can I inspect disappearing element in a browser?, except it's the reverse.
I'm trying to debug which JS adds a bunch of rogue <iframe> aswift_1, aswift_2, etc. elements to the page, like so:
I'd like to use Chrome Devtools (or Firefox) to pause execution as soon as such an element is added and inspect the call stack, hopefully finding the culprit.
Other ideas are welcome as well.
You can use this simple chrome extension.
It will trigger the debugger AFTER element with id matching aswift_ is added(of course you need to open chrome dev tools first).
https://gist.github.com/maciejmackowiak/8043c8630004644144711f730ef45f1b
To activate this extension download -> unpack, open manifest.json and in line 8 change the example.com to the domain you want to inspect.
Then go to chrome://extensions/
Click on Developer mode and Load unpacked
When you will go to the page maching the domain this should show up after element with id starting with aswift_ is added:
Paused in debugger
Now you can use "step over next function call(F10)" (you may need to hit it few times before it will loop thru all mutations and "go" to another function)
Quickest way in chrome would be to take a look at either the network tab (for response) or do a global search using Ctrl+Shift+F on Windows and look for certain tags used in those elements which are being added to the DOM

How to inspect the CSS file in Angular 5 or line to change CSS if we want any CSS?

I am making project in Angular 5. When I inspect the CSS there is tag. If we want to change or find out that CSS is coming from which file basically or which line number of that file (like we do in normal html page). How can I find it?
With angular, the CSS is injected straight into the html page during compilation.
Therefor it's not possible to see where the CSS came from with the DevTools.
However, it is possible to see the CSS styles applied to an element, if you want to find out which file it came from, you will have to search the project.(ctrl+shift+f)
If you're using Google Chrome, you can use the developer console. Safari has a similar interface, but I'm not as familiar with that.
To access the developer console on Chrome, use keys Cmd-Option-I and you should see a console pop up on the right of your browser window. Next, if it isn't already selected, select the Elements tab at the top. You should now see the html that your browser rendered into the site.
Next, you'll want to select the element you want to inspect. You can either do this by accessing the nested HTML structure directly, or if you select the mouse icon at the top left of the window, you can select an element on the webpage and it will be expanded in the developer console.
Once you've selected an element (it should be grey/blue highlighted), you can see its styling at the bottom of your tab. The styling is in decreasing hierarchy order: the top elements override the bottom ones. You can see the styling is grouped based off of the id/class that it is applied to and at the top of each section is the file line of from which the styling came. Click that link to see the file!

Use `inspect an element` tool to make global Javascript variable

In the modern browsers (and Firefox + firebug) you can open up your developer tools and either go to the HTML/Elements tab, or you can inspect an element to get an interactive DOM display.
You can do heaps of useful stuff here, and one thing I need to do often is manipulate/interact with a particular element using Javascript (in the console tab).
What I've been doing is using the interactive DOM to add an id attribute like 'bob', then doing whatever I need to do in the console with document.getElementById('bob') (or $('#bob')).
Ideally what I'd like to do is right click on the element and select an option like make JS variable so in the console I can just use a variable called bob,does anyone know of any such hidden feature/addon/faster method?
Use the built-in XPath context menu as a shortcut. Here is the process:
Right Click on an element from within the DOM Inspector tab
Select Copy XPath from the context menu
Paste selector into the console
Enclose the selector in single quotes
Enclose the selector in the $x() method of the console API
Append the array index of the selector match
Append the desired property/method reference
$x('//*[#id="header"]')[0].innerText
You can now use the $0 variable in the devtools to get a DoM reference to the last element you selected in the Elements pane.
https://developer.chrome.com/devtools/docs/commandline-api#0-4
NB This works in Firefox, too!