Conflicting scripts

Hi

I added the following code to the bottom of some pages:

 <script>
function init() {
var imgDefer = document.getElementsByTagName('img');
for (var i=0; i<imgDefer.length; i++) {
if(imgDefer[i].getAttribute('data-src')) {
imgDefer[i].setAttribute('src',imgDefer[i].getAttribute('data-src'));
} } }
window.onload = init;
</script>

One of the results is that the image from another script stopped showing

<script type="text/javascript" src="/Pinto/scripts/share42.js"></script>

See the difference in http://pintotours.net/Americas/DomRepublic/MeliaCaribe.html

If you take out the script above the share42 shows; if you include it it does not show.

I have no idea about javascript so any help much appreciated.

You can only have one onload event attached to the window object. Combine them :slight_smile: .

Hi Ryan.

How?..

I’m a bit busy working (I know it doesn’t look like it since I’m here). Just wanted to give a pointer. Now whoever comes in this thread can help you combine it. Try messing with thingsand see where that takes you though.

Should I just add one at the end of the other…?

This is the share42.js:

/* share42.com | 28.05.2014 | (c) Dimox */
window.onload=function(){var e=document.getElementsByTagName('div');for(var k=0;k<e.length;k++){if(e[k].className.indexOf('share42init')!=-1){if(e[k].getAttribute('data-url')!=-1)var u=e[k].getAttribute('data-url');if(e[k].getAttribute('data-title')!=-1)var t=e[k].getAttribute('data-title');if(e[k].getAttribute('data-image')!=-1)var i=e[k].getAttribute('data-image');if(e[k].getAttribute('data-description')!=-1)var d=e[k].getAttribute('data-description');if(e[k].getAttribute('data-path')!=-1)var f=e[k].getAttribute('data-path');if(e[k].getAttribute('data-icons-file')!=-1)var fn=e[k].getAttribute('data-icons-file');if(!f){function path(name){var sc=document.getElementsByTagName('script'),sr=new RegExp('^(.*/|)('+name+')([#?]|$)');for(var p=0,scL=sc.length;p<scL;p++){var m=String(sc[p].src).match(sr);if(m){if(m[1].match(/^((https?|file)\:\/{2,}|\w:[\/\\])/))return m[1];if(m[1].indexOf("/")==0)return m[1];b=document.getElementsByTagName('base');if(b[0]&&b[0].href)return b[0].href+m[1];else return document.location.pathname.match(/(.*[\/\\])/)[0]+m[1];}}return null;}f=path('share42.js');}if(!u)u=location.href;if(!t)t=document.title;if(!fn)fn='icons.png';function desc(){var meta=document.getElementsByTagName('meta');for(var m=0;m<meta.length;m++){if(meta[m].name.toLowerCase()=='description'){return meta[m].content;}}return'';}if(!d)d=desc();u=encodeURIComponent(u);t=encodeURIComponent(t);t=t.replace(/\'/g,'%27');i=encodeURIComponent(i);d=encodeURIComponent(d);d=d.replace(/\'/g,'%27');var fbQuery='u='+u;if(i!='null'&&i!='')fbQuery='s=100&p[url]='+u+'&p[title]='+t+'&p[summary]='+d+'&p[images][0]='+i;var s=new Array('"#" data-count="fb" onclick="window.open(\'http://www.facebook.com/sharer.php?m2w&'+fbQuery+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0\');return false" title="Share on Facebook"','"#" data-count="gplus" onclick="window.open(\'https://plus.google.com/share?url='+u+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0\');return false" title="Share on Google+"','"#" data-count="lnkd" onclick="window.open(\'http://www.linkedin.com/shareArticle?mini=true&url='+u+'&title='+t+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=600, height=400, toolbar=0, status=0\');return false" title="Share on Linkedin"','"#" data-count="pin" onclick="window.open(\'http://pinterest.com/pin/create/button/?url='+u+'&media='+i+'&description='+t+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=600, height=300, toolbar=0, status=0\');return false" title="Pin It"','"#" data-count="twi" onclick="window.open(\'https://twitter.com/intent/tweet?text='+t+'&url='+u+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0\');return false" title="Share on Twitter"');var l='';for(j=0;j<s.length;j++)l+='<a rel="nofollow" style="display:inline-block;vertical-align:bottom;width:24px;height:24px;margin:0 6px 6px 0;padding:0;outline:none;background:url('+f+fn+') -'+24*j+'px 0 no-repeat" href='+s[j]+' target="_blank"></a>';e[k].innerHTML='<span id="share42">'+l+'</span>';}};};

Nothing so terrible would happen that if you tried that, you couldn’t easily undo it (in other words, why not try?)

First reason: I cannot read the thing as it is! Look above!

Ok. I’ve unminified it and this is what I got

/* share42.com | 28.05.2014 | (c) Dimox */
window.onload = function() {
	var e = document.getElementsByTagName('div');
	for (var k = 0; k < e.length; k++) {
		if (e[k].className.indexOf('share42init') != -1) {
			if (e[k].getAttribute('data-url') != -1) var u = e[k].getAttribute('data-url');
			if (e[k].getAttribute('data-title') != -1) var t = e[k].getAttribute('data-title');
			if (e[k].getAttribute('data-image') != -1) var i = e[k].getAttribute('data-image');
			if (e[k].getAttribute('data-description') != -1) var d = e[k].getAttribute('data-description');
			if (e[k].getAttribute('data-path') != -1) var f = e[k].getAttribute('data-path');
			if (e[k].getAttribute('data-icons-file') != -1) var fn = e[k].getAttribute('data-icons-file');
			if (!f) {
				function path(name) {
					var sc = document.getElementsByTagName('script'),
					sr = new RegExp('^(.*/|)(' + name + ')([#?]|$)');
					for (var p = 0, scL = sc.length; p < scL; p++) {
						var m = String(sc[p].src).match(sr);
						if (m) {
							if (m[1].match(/^((https?|file)\:\/{2,}|\w:[\/\\])/)) return m[1];
							if (m[1].indexOf("/") == 0) return m[1];
							b = document.getElementsByTagName('base');
							if (b[0] && b[0].href) return b[0].href + m[1];
							else return document.location.pathname.match(/(.*[\/\\])/)[0] + m[1];
						}
					}
					return null;
				}
				f = path('share42.js');
			}
			if (!u) u = location.href;
			if (!t) t = document.title;
			if (!fn) fn = 'icons.png';
			function desc() {
				var meta = document.getElementsByTagName('meta');
				for (var m = 0; m < meta.length; m++) {
					if (meta[m].name.toLowerCase() == 'description') {
						return meta[m].content;
					}
				}
				return '';
			}
			if (!d) d = desc();
			u = encodeURIComponent(u);
			t = encodeURIComponent(t);
			t = t.replace(/\'/g, '%27');
			i = encodeURIComponent(i);
			d = encodeURIComponent(d);
			d = d.replace(/\'/g, '%27');
			var fbQuery = 'u=' + u;
			if (i != 'null' && i != '') fbQuery = 's=100&p[url]=' + u + '&p[title]=' + t + '&p[summary]=' + d + '&p[images][0]=' + i;
			var s = new Array('"#" data-count="fb" onclick="window.open(\'http://www.facebook.com/sharer.php?m2w&' + fbQuery + '\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0\');return false" title="Share on Facebook"', '"#" data-count="gplus" onclick="window.open(\'https://plus.google.com/share?url=' + u + '\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0\');return false" title="Share on Google+"', '"#" data-count="lnkd" onclick="window.open(\'http://www.linkedin.com/shareArticle?mini=true&url=' + u + '&title=' + t + '\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=600, height=400, toolbar=0, status=0\');return false" title="Share on Linkedin"', '"#" data-count="pin" onclick="window.open(\'http://pinterest.com/pin/create/button/?url=' + u + '&media=' + i + '&description=' + t + '\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=600, height=300, toolbar=0, status=0\');return false" title="Pin It"', '"#" data-count="twi" onclick="window.open(\'https://twitter.com/intent/tweet?text=' + t + '&url=' + u + '\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=100, top=100, width=550, height=440, toolbar=0, status=0\');return false" title="Share on Twitter"');
			var l = '';
			for (j = 0; j < s.length; j++) l += '<a rel="nofollow" style="display:inline-block;vertical-align:bottom;width:24px;height:24px;margin:0 6px 6px 0;padding:0;outline:none;background:url(' + f + fn + ') -' + 24 * j + 'px 0 no-repeat" href=' + s[j] + ' target="_blank"></a>';
			e[k].innerHTML = '<span id="share42">' + l + '</span>';
		}
	};
};

Help…

Hi there qim,

if the script precedes the closing body, then just use this…

[code]

[/code]

coothead

Hi coothead

Thanks, but it does not seem to defer the images any longer

The share42 image is back but the Air France advert does not defer (I think according to FF Web Console.

Hi there qim,

I cannot see why, if, as you say, the script precedes the closing body
tag, then the document has loaded by the time that your script is run. :grinning:

Nevertheless, you can use this…

[code]

[/code]

coothead

Magic!

It works!

Many thanks.

Well there’s always…

“more than one way to skin a cat” :cool:

coothjead

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.