admin管理员组文章数量:1277512
I was wondering if it is possible to add a meta tag using jquery to an html page before the page loads.
The reason why I ask, is because I have a page with no viewport meta tag on and it should have it only when the resolution drops below 700px - <meta name="viewport" content="width=device-width, initial-scale=1"/>
The reason being, I have html markup for mobile site (using media queries) & also html markup for desktop version ( I don't have one for tablets). I want to make sure html markup designed for desktop is rendered properly on tablet on page load and also when we change the device orientation.
Thanks in advance!
I was wondering if it is possible to add a meta tag using jquery to an html page before the page loads.
The reason why I ask, is because I have a page with no viewport meta tag on and it should have it only when the resolution drops below 700px - <meta name="viewport" content="width=device-width, initial-scale=1"/>
The reason being, I have html markup for mobile site (using media queries) & also html markup for desktop version ( I don't have one for tablets). I want to make sure html markup designed for desktop is rendered properly on tablet on page load and also when we change the device orientation.
Thanks in advance!
Share Improve this question asked Aug 27, 2014 at 9:16 AB.AB. 551 gold badge2 silver badges8 bronze badges 3-
@AndyHolmes - Having a viewport
<meta name="viewport" content="width=device-width, initial-scale=1"/>
by default is causing the page to overflow on tablets when we change the orientation multiple times. – AB. Commented Aug 27, 2014 at 9:22 - Realised that after my ment! haha. Check my answer :) – Andy Holmes Commented Aug 27, 2014 at 9:22
- You should be handling this with CSS. If the viewport tag is causing issues on tablets or desktops then your CSS is badly coded! – Cameron Commented Aug 27, 2014 at 9:24
3 Answers
Reset to default 7Using jQuery you can do it like the following:
if ($(window).width() < 700) {
$('head').append('<meta name="viewport" content="width=device-width, initial-scale=1"/>');
}
EDIT
To have the viewport tag by default, then remove it above 699px:
HTML:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
jQuery:
if ($(window).width() > 699) {
$('head').remove('<meta name="viewport" content="width=device-width, initial-scale=1"/>');
}
Try this (you don't need the /
at the end of the meta tag anymore):
if(screen.width < 700) {
$('head').append('<meta name="viewport" content="width=device-width, initial-scale=1">');
}
Sure you can as soon as the jQuery library is loaded and the head is registered at the DOM:
if (jQuery('html').outerWidth(true) < 700) {
jQuery.('head').append('<meta name="viewport" content="width=device-width, initial-scale=1"/>');
}
本文标签: javascriptchange viewport meta tag with jqueryStack Overflow
版权声明:本文标题:javascript - change viewport meta tag with jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291405a2370569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论