Click here to Skip to main content
15,923,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone. I want to try get meta tags from website. Maybe someone knows how I can do this?

What I have tried:

I'm trying with JS code like this:
JavaScript
$.ajax({
    url: "http://example.com",
    dataType: 'text',
    success: function(data) {
        var elements = $("meta").html(data)[0].getElementsByTagName("head");
        for(var i = 0; i < elements.length; i++) {
            var theText = elements[i].firstChild.nodeValue;
            console.log(theText);
        }
    }
});

But, I get this error Failed to load http://example.com: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
Posted
Updated 28-Aug-18 10:23am
v2
Comments
Dave Kreskowiak 28-Aug-18 16:06pm    
Quote:How I can get head from another site ?

LOL. Did you read this before you posted it? I don't think this is the site for that.
Richard Deeming 28-Aug-18 16:24pm    
Fixed the title - happy now? :)
Dave Kreskowiak 28-Aug-18 17:03pm    
Happiness is no longer a possibility. I'm married. :)


1 solution

You can't do that in pure Javascript for security reasons. You'll need to use a proxy script on your own site, which issues a request to the target site, reads the metadata, and returns it to your script.

How you do that will depend on which server-side language you're using. For example, with ASP.NET:
Reading Meta Tags of Any Page Programatically without loading in browser[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900