Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery selector for <link> elements in <head>

Tags:

jquery

dom

We use jQuery to parse some HTML. I then need to traverse that document and find some elements. Among the elements I need to find, there are the <link> elements.

This works perfectly well to extract all the <a> elements:

$(string).find("a")

but this doesn't work to extract the <link> elements :

$(string).find("link")

The string parameter is the html content (e.g. received on a request).

Any idea why? (I guess that the find only applies to the <body> elements). Also, any idea on how to actually extract these <link> elements?

like image 910
Julien Genestoux Avatar asked Jun 20 '11 20:06

Julien Genestoux


People also ask

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

How do I get all HTML elements in jQuery?

To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.

Can you select all elements using jQuery How?

With jQuery selectors, you can find or select HTML elements based on their id, classes, attributes, types and much more from a DOM. In simple words, you can say that selectors are used to select one or more HTML elements using jQuery and once the element is selected then you can perform various operation on that.

Which of the following jQuery selector selects all elements available in a dom?

Description: Selects all elements.


2 Answers

From the documentation of the feature you're using for $(string) (which is the function jQuery( html, [ownerDocument] )):

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, we use the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>, <title>, or <head> elements. As a result, the elements inserted may not be representative of the original string passed.

Try not to use jQuery to manipulate entire HTML documents.

Note, in particular, that a link node in a standalone snippet of HTML can be "found" just fine.

like image 126
Lightness Races in Orbit Avatar answered Oct 12 '22 21:10

Lightness Races in Orbit


Well, based on what I can find in the source code of jQuery, the engine itself will not create tags (or fragments) that are not "properly seated". Even when passing a string, jQuery recognizes that the header has already been supplied and will not generate it.

After all, when jQuery is passed a string of HTML, it's actually calling document.createElement and creating an array list of those elements.

EDIT: After a little more investigation, it looks like it's the browser actually limiting element creation, not jQuery. Either way, you're left with absent tags. Which brings me to my same conclusion below.

As much as I don't like it, may be time for regex/string manipulation.

like image 25
Brad Christie Avatar answered Oct 12 '22 22:10

Brad Christie