简体   繁体   English

阻止加载DOM对象

[英]preventing a DOM object from loading

I know it is possible to set a display property to "none" and toggle it with jquery, but ultimately the object is still loaded, just not displayed. 我知道可以将display属性设置为“none”并使用jquery切换它,但最终仍然加载了对象,只是没有显示。 Is there a way to prevent a element from being loaded at all to save loading times? 有没有办法防止元素被加载以节省加载时间? I am using the below code to toggle css properties but I want it to not load the element at all. 我使用下面的代码来切换css属性,但我希望它根本不加载元素。

js JS

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) {
    $(".startb").css({"display":"inline-block"});
    $(".flashObj").css({"display":"none"});
}
else {
    $(".startb").css({"display":"none"});
    $(".flashObj").css({"display":"inline-block"});
}

my elements 我的元素

<div class="startb"><a href="Audio/004_IAM_God_is_Love.mp3"><img src="dbs/images/start.png" width="40" height="40" /></a></div>

<div class="flashObj"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="200" height="20">
            <param name="movie" value="dbs/js/singlemp3player.swf?file=Audio/004_IAM_God_is_Love.mp3&autoStart=false&backColor=000000&frontColor=ffffff&songVolume=90" />
            <param name="wmode" value="transparent" />
            <embed wmode="transparent" width="200" height="20" src="dbs/js/singlemp3player.swf?file=Audio/004_IAM_God_is_Love.mp3&autoStart=false&backColor=000000&frontColor=ffffff&songVolume=90"
            type="application/x-shockwave-flash" />
            </object></div>

Dont put the element on the page and create it when you need it. 不要将元素放在页面上并在需要时创建它。

If you have a html element in the html then its already there. 如果你在html中有一个html元素,那么它已经存在了。 This should not be much of an overhead, unless you have numerous versions of the element. 除非你有许多版本的元素,否则这不应该是一个很大的开销。

Put this at the end of Your html file: 把它放在你的html文件的末尾:

<script type="text/template" id="myContent">
    <div>content</div>
</script>

Then append the element like this: 然后追加这样的元素:

$('#myContent').appendTo('body');

By this You will save time the element is rendered. 这样您将节省元素渲染的时间。 But still You have to download the html. 但是你还是要下载html。 Another way to do this would be downloading the content by AJAX. 另一种方法是通过AJAX下载内容。

You can delete it from the page: 您可以从页面中删除它:

$(".startb").remove();

But it will still be loaded and then deleted. 但它仍然会被加载然后删除。
So instead of delete it when not needed, create it when needed... 因此,不需要在不需要时删除它,在需要时创建它...

You can use AJAX loading as follows, but you have to split then into three different HTML files and one JS file 您可以按如下方式使用AJAX加载,但必须将其拆分为三个不同的HTML文件和一个JS文件

JS JS

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) {
$("#container").empty();
$("#container").load( "startb.html");

}
else {
$("#container").empty();
$("#container").load( "flashObj.html");
}

startb.html startb.html

<div class="startb"><a href="Audio/004_IAM_God_is_Love.mp3"><img src="dbs/images/start.png" width="40" height="40" /></a></div>

flashObj.html flashObj.html

<div class="flashObj"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="200" height="20">
            <param name="movie" value="dbs/js/singlemp3player.swf?file=Audio/004_IAM_God_is_Love.mp3&autoStart=false&backColor=000000&frontColor=ffffff&songVolume=90" />
            <param name="wmode" value="transparent" />
            <embed wmode="transparent" width="200" height="20" src="dbs/js/singlemp3player.swf?file=Audio/004_IAM_God_is_Love.mp3&autoStart=false&backColor=000000&frontColor=ffffff&songVolume=90"
            type="application/x-shockwave-flash" />
            </object></div>

And your homepage: 和你的主页:

<div id="container"></div>

Please tell me if you need any further help? 如果您需要任何进一步的帮助,请告诉我?

You can use lazy loading: so for example you put in the html.. and you have a condition in your javascript to replace the data-src with src when the width changes below 768px. 你可以使用延迟加载:例如你输入html ..你的javascript中有一个条件,当宽度变化低于768px时,用src替换data-src。 some libraries for lazy loading 一些用于延迟加载的库

No. 没有。

Some kinds of elements load regardless of what you do. 无论你做什么,某些元素都会加载。

var img = document.createElement('img');
img.onerror = function () { alert("The browser tried to load me and failed"); };
img.src = 'bogus';
// img hasn't even been added to the DOM.

You could use a server-sided language, such as PHP, to load the parts of the DOM. 您可以使用服务器端语言(如PHP)来加载DOM的各个部分。 While it isn't always an option, it is far more reliable than a client-side script. 虽然它并不总是一个选项,但它比客户端脚本更可靠。 The downside is that it may be more difficult to debug. 缺点是它可能更难调试。

Let's assume there is a div element like this in your page. 我们假设你的页面中有一个这样的div元素。

<div id="containerDiv">
</div>

You can add your elements on page load by using the $("#containerDiv").append() method. 您可以使用$("#containerDiv").append()方法在页面加载时添加元素。 However, I don't see the reason why you wouldn't do it on the server side. 但是,我没有看到你不在服务器端这样做的原因。

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

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