Forum Moderators: open
if ($('#foo').length) {
$('#foo').hide();
} if ($('#foo').length) {
$(this).hide();
}]
if ((foo = $('#foo')).length) {
foo.hide();
} var a = $('#foo');
if (a.length)
a.hide(); I hope it's also obvious that these are micro optimizations and that no-one's going to notice the difference.
var a = $('#foo');
if (a.length)
a.hide();
~1.5s is certainly not micro ;-)
There's an error in the fiddle, (a = $('#foo').length) should read ((a = $('#foo')).length).
You don't need to include var a; either.
You don't need to include var a; either.
Does that still not make a second DOM query? (this refers to assigning a queried element to a variable)
$('#foo').someMethod(
$(this).doSomething()
)
var a = $('#foo');
if(a.length) {
a.hide();
} if((a = $('#foo')).length) {
a.hide();
} if($('#foo').length) {
$('#foo').hide();
}
If you're referring to my fiddle,
$(function() {
var a;
// Expected a conditional expression and instead saw an assignment
if (a = $('#foo').length)
a.hide();
});
for (i=0; i < 10; i++) {
if ((a = $('#foo')).length)
a.hide();
} a = [];
for (i=0; i < 10; i++) {
if ((a[i] = $('#foo')).length)
a[i].hide();
}
for (let i=0; i < 10; i++) {
const a = $('#foo');
if (a.length) a.hide;
}
function hideElement(elem) {
var a = $('#' + elem);
var i;
for (i=0; i < 10; i++) {
if (a.length) a.hide;
}
}
hideElement('foo');
$bCount = automagically figured out in advance;
echo <<<EOF
<div id="banner_1_$bCount"></div>
<div id="local_$bCount"></div>
<div id="banner_2_$bCount"></div>
<div id="widget_$bCount"></div>
<script>showBanner($bCount);</script>
EOF;
<body>
<div id="page1">
<h1>page1</h1>
<div class="banner banner-top">banner-ad</div>
<p> page 1 content blah bla bla!</p>
<div class="banner banner-bottom">banner-ad</div>
</div>
<div id="page2">
<h1>page2</h1>
<div class="banner banner-top">banner-ad</div>
<p> page 2 content yadda yada yaddah!</p>
<div class="banner banner-bottom">banner-ad</div>
</div>
<etc...>
</body>
//to select all banner ads
var allBanners = document.querySelectorAll('.banner');
// this is a node list of all the .banner nodes, you can then loop through each one refreshing each one.
// to select banners on page 1
var pageOne = document.querySelector('#page1');
// selects the page1 node
var pageOneBanners = pageOne.querySelectorAll('.banner');
// as above this is a node list of all the .banner nodes but only those on page 1.
googletag.cmd.push(function() {
slot[x] = googletag.defineSlot('/12345/' + atf_rect,
[300, 250], 'banner_' + i + '_' + bCount) // 'banner_' + i + '_' + bCount = banner_1_$bCount
.addService(googletag.pubads());
googletag.display('banner_' + i + '_' + bCount);
googletag.pubads().refresh([slot[bCount]]);
}