We help IT Professionals succeed at work.

Change meta tags from a function

Dada44
Dada44 asked
on
Hi all,

My page has already the following meta tag inside of the head section:
<meta name='description' content=''>

But at certain event (when entering a function Facebook related) I would like to change it and add these two:
 
<meta name="title" content="some about the content" />
<meta name="description" content="my web description" />
<link rel="image_src" href="http://myimage.gif" / >

How can I do that?
Thanks a lot
Comment
Watch Question

Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Dada44,
Try this : 
<script type="text/javascript" language="javascript">
    function alertz() {
        //debugger;
        var meta = document.createElement('meta');
        meta.name = 'title';
        meta.content = 'title';
        document.getElementsByTagName('head')[0].appendChild(meta);
        meta = document.createElement('meta');
        meta.name = 'description';
        meta.content = 'description';
        document.getElementsByTagName('head')[0].appendChild(meta);
    }
</script>

Call alertz function in the page_load
Regards,
Chinmay


Author

Commented:
whoah! seems to work bit what about this one:
<link rel="image_src" href="http://myimage.gif" / > hoe can it be added??
Thanks!!
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
It is also simple and can be done in the same way but here is a different approach you might like this for other usage as well :
http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
And you are welcome. :)

Author

Commented:
So if I want to achieve this:
<link rel="image_src" href="http://myimage.gif" / >

I do:
  <script type="text/javascript" src="jquery.min.js"></script>
    <script>
  $(document).ready(function () {

    addface();
});

function addface(){
var filename="http://myimage.gif";
var fileref=document.createElement("link")
  fileref.setAttribute("rel", "image_src")
  fileref.setAttribute("href", filename)
}
</script>

I don't know if it's correct, how can I check it?
Thanks a ton ;)
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
You can verify it by DOM as such if you are not getting an error this code works. BTW, why everything needs to be converted to JQuery? :)

Author

Commented:
thanks!