How to detect meta tag using jQuery in HTML, DOM

Here is the problem I ran into using a PHP script to grab an HTML page and return the HTML content as a string in a jQuery AJAX call.

$.ajax({ type: "POST", data: "url="+tar_url, url: "curl.php", success: function (data, textStatus, jqXHR){ $("meta", $(data))// not working $(data).find("meta") //not working $("img",$(data)) //works $(data).find("div") //works }, 

In the success function callback, I noticed that I can use regular selectors to get div, img, ul, etc. However, none of the above methods could choose meta tags.

First, I don’t know if HTML contains any meta tags. If it contains some, I would like to select them and analyze them, etc. Can't select those meta tags using jQuery?

+4
source share
4 answers

I had the same problem just now, and I came across this message when searching for a solution. In the end, filter( ) worked for me instead of find( ) .

From the Chrome Javascript console:

 $.ajax({ type: 'GET', url: $getThisURL, success: function(data) { output = $(data).filter('meta'); } }); 

Output:

 [<meta http-equiv=​"Content-Type" content=​"text/​html;​charset=utf-8">​, <meta name=​"color:​Background" content=​"#262626">​, <meta name=​"color:​Text" content=​"#fff">​, <meta name=​"color:​Links" content=​"#ffbc00">​, <meta name=​"if:​Show notes" content=​"1">​, <meta name=​"robots" content=​"noindex">​, <meta charset=​"utf-8">​, <meta http-equiv=​"x-dns-prefetch-control" content=​"off">​] 
+5
source

you need to try something like this

 var author = $('meta[name=author]').attr("content"); 

some other examples

 $("meta[property=og:title]").attr("content", document.title); $("meta[property=og:url]").attr("content", location.toString()); 

another example from google

 var mt = $('meta[name=some-name]'); mt = mt.length ? mt : $('<meta name="some-name" />').appendTo('head'); mt.attr('content', 'some value'); 
+2
source

When accessing facebook meta tags: If you get

 "uncaught exception: Syntax error, unrecognized expression: [property=fb:app_id]" 

You need to specify the FB meta tag property name:

 myApp2 = $('meta[property="fb:app_id"]').attr("content") ; 
+2
source

You need to use javascript escape function to send HTML tags in jquery, use this to select meta tag

 escape( $(data).find("meta") ) 
0
source

All Articles