0

How can I, with JavaScript, check if a webpage has any of the following:

<meta name="viewport" content="width=device-width, initial-scale=1.0 maximum-scale=1, user-scalable=no" />

<meta name="viewport" content="initial-scale=1.0 maximum-scale=1" />

<meta name="viewport" content="maximum-scale=1" />

<meta name="viewport" content="maximum-scale=1.0" />

<meta name="viewport" content="user-scalable=no" />

and if any of this is in the source, then run certain JavaScript code like in the suite of an if-condition.

Basically what I am trying to do is check if zooming is disabled in anyway and if so, execute certain code and if not the execute other code.

Irfan Mir
  • 2,107
  • 8
  • 27
  • 33
  • 1
    var nodes = $('meta[name=viewport]'); $(nodes).each(function(index, nodeValue){ var content = $(nodeValue).attr('content');// check nodeValue here }); – vijay Apr 26 '13 at 07:58
  • @VijayPatel can you explain how to check nodeValue for each of the elements in the index in an answer? – Irfan Mir Apr 26 '13 at 08:08
  • @Irfan Please check my post – vijay Apr 26 '13 at 09:27

2 Answers2

0

You can use

function readViewportValues(){
    var metas = $('meta[name=viewport]');
    var properties = {};

    function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }

    function readItem(index, text) {
        var parts = jQuery.trim(text).split(' ');
        console.log(parts);
        if (parts.length > 1) {
            $.each(parts, readItem);
        } else {
            parts = parts[0].split('=');
            var key = jQuery.trim(parts[0]),
                value = parts[1];
            value = isNumber(value) ? parseFloat(value) : value;
            properties[key] = value;
        }
    }

    metas.each(function () {
        var content = $(this).attr('content').split(',');
        $.each(content, readItem);
    });
    return properties;
}


// usage
var viewport = readViewportValues();
if (viewport['maximum-scale'] === 1){
    // do something
}

This will return an object with the found properties and you can query it..

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0
var nodes = $('meta[name=viewport]'); 
$(nodes).each(function(index, nodeValue){ 
     var content = $(nodeValue).attr('content');// check nodeValue here 
     // You will get values of content from each meta element here in content variable ex. "user-scalable=no"
     // so now based on these values you can so do any processing you want
}); 
vijay
  • 1,323
  • 1
  • 11
  • 15