1

Im trying to show different divs based on meta keywords.

This is my html:

<div id="sv"></div>
<div id="fi"></div>
<div id="uk"></div>
<div id="dk"></div>

Meta tag, which changes dynamically as the divs above:

<meta name="keywords" lang="fi">

Im using a javascript to do the exact same thing bases on title tags, and would love if it would work for meta tags aswell:

$('document').ready(
    function () {
        function _title() {
            var allow = document.title.search("text");
            if (allow > -1) {
                document.getElementById('demo').style.display = "block";
            }
        }
        window.onload = _title;
    }
);

This script also needs the div (demo) to be set as display none for reference if someones sees this and trying to use it. I cant use jquery either.

Infotheek SE
  • 113
  • 1
  • 1
  • 9
  • This is an example of how to read meta tags: http://stackoverflow.com/a/7524621/1389366 – icke Feb 05 '15 at 10:45
  • possible duplicate of [Is it possible to use jQuery to read meta tags](http://stackoverflow.com/questions/1036351/is-it-possible-to-use-jquery-to-read-meta-tags) – gvee Feb 05 '15 at 12:14
  • Should have stated that i need to use vanilla js only, sorry. – Infotheek SE Feb 05 '15 at 12:17

1 Answers1

0

This code may help you:

<meta name="text" property="text" content="This is text" />
<meta name="video" property="text" content="http://video.com/video33353.mp4" />

Script:

var x = document.getElementsByTagName("META");
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        if (x[i].name=="video")
        {
            alert(x[i].content);  // HERE YOU CAN ADD YOU ACTION
         }
    }    

Sample jsfiddle: http://jsfiddle.net/muthupandiant/ogfLwdwt/

Nelson Teixeira
  • 5,856
  • 4
  • 31
  • 60
muTheTechie
  • 768
  • 9
  • 20
  • Hmm thanks. tried to change document.title.search to the tagname property. cant get it to work though. – Infotheek SE Feb 05 '15 at 10:58
  • Sorry, this inst clear for me. Tried to fiddle around with my code and the provided, cant get it to work – Infotheek SE Feb 05 '15 at 11:49
  • This is pretty close. I need to target lang="sv" and not name="video" though. Thats the last puzzlepart for me – Infotheek SE Feb 05 '15 at 12:21
  • Oh that works! Fantastic. How would i do to set it for more variables since I need to change divs depending on all the examples in my post? Also update your original answer so i can accept it. @Base M Pandian – Infotheek SE Feb 06 '15 at 07:21