I want to extract <p>
content in a html attributes, <meta content='**I want to extract here**' name='description'/>
. How can be done the job.
I am using blogger.com, So in blogger.com I want to extract the <p>
content in meta tag. Please help me. I tried a lot but failed.
Asked
Active
Viewed 626 times
2

Mr Lister
- 45,515
- 15
- 108
- 150

Mehrab Tamim
- 71
- 1
- 6
2 Answers
1
Try this:
Add the Id metaTag
to the tag meta
var metaTag = document.getElementById("metaTag").content;
document.write(metaTag);

Momin
- 3,200
- 3
- 30
- 48

Wildin Mota
- 11
- 4
1
Run this JavaScript on document load.
// JavaScript
var p = document.getElementsByTagName("p")[0];
console.log(p.textContent);
var metaTag = document.querySelector('meta[name="description"]');
console.log(metaTag);
metaTag.setAttribute("content",p.textContent);
console.log(metaTag);
<!-- Sample HTML -->
<meta content='**I want to extract here**' name='description'/>
<p>some text</p>

vatz88
- 2,422
- 2
- 14
- 25
-
It does not work. Unable to retrive data from `
` tag in content attributes.
– Mehrab Tamim Apr 23 '17 at 18:04 -
JavaScript should run on document load. Add the JavaScript code inside ` – vatz88 Apr 23 '17 at 18:05
-
Yes, here working. But when I put it into blogger.com in head or body tag it looking something this, `` I mean, `"` converts to `"`. If I write a post and put some text into `
` it is not extracting into meta content atributes. and exacly I want that.
– Mehrab Tamim Apr 23 '17 at 18:14 -
May be the website doesn't allow `scripts` inside its content for reason of [XSS attack](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)). – vatz88 Apr 23 '17 at 18:22
-
Also i don't understand what's the use of modifying the meta tag on document load. Meta tag are used by web crawlers, search engines. So changing it on document load is really of no use. – vatz88 Apr 23 '17 at 18:27
-
I tried it now with Notepad++ its does not working. But how works here in this websites.? I want to do it in order to create meta description for web crawlers – Mehrab Tamim Apr 23 '17 at 18:32
-
I just joined jsfiddle.net . But I dont know how to share. Please help me , I badly need it. – Mehrab Tamim Apr 23 '17 at 18:44
-
Oh! got it. the link is, https://jsfiddle.net/mehrabtamim/upf21h3f/1/ . – Mehrab Tamim Apr 23 '17 at 18:46
-
You added same code as mine in the answer and it is working if you open the console and check. – vatz88 Apr 23 '17 at 18:59
-
Yes! I added the same code. But I am unable to get the output. How to open console? – Mehrab Tamim Apr 23 '17 at 19:10
-
-
-
-
Bro, One more question. In console the code works. But can't i show it in head section of the page source? I mean the meta tag does not show in page source within head section. Any way to solve it? – Mehrab Tamim Apr 24 '17 at 08:38
-
It's dynamically modified. You won't see the change in source file. See the source code in developer console, it'll show the change. – vatz88 Apr 24 '17 at 10:31
` tag?
– GalacticCowboy Apr 23 '17 at 02:10` tag and put it into a `` tag.
– Mehrab Tamim Apr 23 '17 at 02:53