简体   繁体   English

如何不使用javascript / jquery根据屏幕大小加载某些元素

[英]How to not load certain elements based on screen size with javascript/jquery

I am making a site using responsive web design (via media queries). 我正在使用响应式网页设计(通过媒体查询)制作网站。

I want to have different ads for different screen sizes. 我想针对不同的屏幕尺寸使用不同的广告。 For example, I don't want a 780by90 ad to be displayed when a phone browser visits the site. 例如,我不希望手机浏览器访问该网站时显示780by90广告。

I know that display: none; 我知道显示:没有; isn't an option because the ad would still be loaded and record an impression. 不能选择,因为广告仍会加载并记录一次展示。 This is not an option. 这不是一个选择。

Basically, I need to know how to load the ads in via javascript after the screen resolution has been found. 基本上,我需要知道在找到屏幕分辨率后如何通过javascript加载广告。 I can find the resolution, I just don't know how to add the ads in after finding that. 我可以找到分辨率,找到它之后我只是不知道如何添加广告。

I have tried .html with jquery but it seems to not allow adding script tags into the dom. 我已经尝试过用jquery编写.html文件,但似乎不允许将脚本标签添加到dom中。

Thanks in advance. 提前致谢。

CSS3 Media Queries将根据屏幕大小有选择地输出图像-只要图像在CSS被引用...

If you're using DFP, they have responsive ad functionality built into the googletag object: 如果您使用的是DFP广告管理系统,则它们在googletag对象中内置了自适应广告功能:

https://support.google.com/dfp_premium/answer/3423562?hl=en&ref_topic=4390040 https://support.google.com/dfp_premium/answer/3423562?hl=zh-CN&ref_topic=4390040

you define ad sizes based on the viewable content area of the browser 您可以根据浏览器的可见内容区域定义广告尺寸

If you're using Google DFP Andyl mentioned their method for responsively displaying ads, which is definitely a good place to start. 如果您使用的是Google DFP广告管理系统, Andyl提到了他们的响应式广告显示方法,这绝对是一个不错的起点。 To this I'd add that you need to define size mappings for every slot on the page (even if they're not all responsive) otherwise nothing works properly for some reason. 为此,我要补充一点,您需要为页面上的每个插槽定义大小映射(即使它们并非全部响应),否则由于某种原因,任何操作都将无法正常进行。

Also, if you want to hide ad units when the browser is smaller than a given width/height, that Google article suggests not defining a mapping for browser size [0, 0]. 另外,如果您想在浏览器小于给定的宽度/高度时隐藏广告单元,则该Google文章建议不要为浏览器大小[0,0]定义映射。 However, I found when I did this it actually displayed my largest creative for some reason. 但是,我发现这样做确实由于某种原因显示了我最大的广告素材。 I managed to suppress ads by creating a minimum size mapping that referred to a non-existent creative size, eg. 我设法通过创建一个最小尺寸映射来隐藏广告,该映射涉及一个不存在的广告素材尺寸,例如

.addSize([0, 0], [1, 1])

You can also apply this to any browser size, not just the smallest. 您还可以将其应用于任何浏览器大小,而不仅仅是最小的浏览器大小。 I did write a short post about this, although it doesn't have much more information than I've provided here. 我确实写了一篇简短的文章 ,尽管没有比我在这里提供的更多的信息。

Hope this helps, 希望这可以帮助,

Toby 托比

I'd recommend doing this kind of calculation on the back-end instead of the front-end. 我建议在后端而不是在前端进行这种计算。 You can use libraries such as WURFL or something like Apache Mobile Filter. 您可以使用WURFL之类的库或Apache Mobile Filter之类的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM