So we run google ads on our sites.
And I got thinking, each ad block ( of various sizes ) loads ..
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
eg:
<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
/* ad served */
google_ad_slot = "xxxxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
and other ads, of different slot ( id's ) amd sizes, on the same page also load :
http://pagead2.googlesyndication.com/pagead/show_ads.js
each time. So a page with say three ads on, loads that exact same js file 3 times ..
Removing that script file, adding it ONCE to the head, doesnt work for us.. the ads just dont show.
So is there a method of loading that show_ads.js file just once / page load ?
The Google ads script utilizes document.write(), which means the script must be in the position in the HTML that you want the ad.
However, fear not: the show_ads.js file will only be downloaded by your browser once. The subsequent <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js">s will load from cache.
In fact, the Cache-Control headers tell your browser it can load the file directly from cache for the next hour, so there should be only one trip to pagead2.googlesyndication.com per session, no matter how many pages are viewed.
Related
I have a website that I want to work online and offline. I want to decrease load time, so I minified the slow loading scripts. It didn't work enough. Locally, some of the scripts are taking 4-6 seconds to load.
I know that linking scripts externally speeds it up drastically. I have tried it and it works. But some of the users will not have internet access. Is there a way to link a group of scripts to an external site, and locally if they do not have internet access?
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="js/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="ui/jquery-ui.min.js" type="text/javascript"></script>
You can do something similar to this answer. Basically, the idea is that you attempt to load the Internet based script for jQuery. Afterward, you do a normal script where you check if jQuery === undefined. If it does, you want to load the local copy.
Basically, an example of what you want to do:
<script src="[jQueryURL]" type="text/javascript"></script>
<script src="[dataTablesURL]" type="text/javascript"></script>
<script src="[jQueryUIURL]" type="text/javascript"></script>
<script type="text/javascript">
if(jQuery === undefined) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "js/jquery-1.9.1.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
//repeat for other two scripts here
}
</script>
What will happen is that it will attempt to load the scripts in question, and afterward it will check if jQuery is defined. If it's not, that means the scripts didn't load, so you want to load the local versions and append them to your header.
Ensure that the script declares a global variable you can test for. Then generate a local script loading element if the external script fails to load (which you can determine by testing for that variable).
<script src="http://example.com/example.js"></script>
<script>
if !(window.Example) {
document.write('<script src="../scripts/example.js"><\/script>');
}
</script>
<script type="text/javascript">
if(navigator.onLine)
{
alert("Connected");
}
else
{
alert("Not Connected");
}
</script>
First Check if internet connection exist then load external file other wise load internal file
I have two adserver javascript codes that must be loaded in the head section of our website. The first needs to load only on frontpage and the second needs to load only on other pages.
I use a Joomla! platform and Easy Scripts plugin to add javascript codes to head section. The problem is that this plugin is loaded on the entire website and I'm not able to make it load only on a specific page.
Please, help me!
Sorry for the lack of details. Here are the codes:
<!--START of script to be loaded everywhere-->
<script type="text/javascript" src="//ro.adocean.pl/files/js/ado.js"></script>
<script type="text/javascript">
if(typeof ado!=="object"){ado={};ado.config=ado.preview=ado.placement=ado.master=ado.slave=function(){};}
ado.config({mode: "old", xml: false, characterEncoding: true});
ado.preview({enabled: true, emiter: "ro.adocean.pl", id: "xxxxxxxxxxxxxxxxxxxxxxxxxxx"});
</script>
<!--END of script to be loaded everywhere-->
<!--START of script to be loaded only on frontpage-->
<script type="text/javascript">
ado.master({id: 'yyyyyyyyyyyyyyyyyyyyyyyyy', server: 'ro.adocean.pl' });
</script>
<!-- END of script to be loaded only on frontpage-->
<!--START of script to be loaded only on otherpages-->
<script type="text/javascript">
ado.master({id: 'zzzzzzzzzzzzzzzzzzzzzzzz', server: 'ro.adocean.pl' });
</script>
<!--END of script to be loaded only on otherpages-->
You can use something like modulesanywhere ( https://www.nonumber.nl/extensions/modulesanywhere ) & sourcer ( https://www.nonumber.nl/extensions/sourcerer ) to 1) define where it loads and 2) the other to include php. With those two things you can use the native Joomla functions like addScriptDeclaration ( https://docs.joomla.org/Adding_JavaScript ) to add the script's code to the head.
I'd like to rotate ads at my website without reloading whole website, so each 25 seconds should be loaded new banner. Third party javascript of ad network looks like <script type=text/javascript src='http://adnetwork.tld/banner.js?id=55&user=200'></script>. I can put this code everywhere at my website and in that position is banner loaded.
I am using setTimeout(function, 25000);, but I don't know if code below is the right way how to do it.
var container = document.getElementsByClassName("cont")[0];
container.innerHTML = "<script type=text/javascript src='http://adnetwork.tld/banner.js?id=55&user=200'></script>";
jQuery( "div.cont" ).replaceWith( "<div class='cont'><script type=text/javascript src='http://adnetwork.tld/banner.js?id=55&user=200'></script></div>" );
How can I reload this position via javascript after 25 seconds, so new banner is shown?
UPDATE: (example provided)
<div id="cont">
<script type=text/javascript src='http://adnetwork.tld/banner.js?id=55&user=200'></script>
</div>
Just use scriptTag.src = "linkYouWantForYourAd"
You can get the script tag by giving it an id or a class name
I have 6 different stylesheets, and 1 html page. What i need to do is have the page load one of the stylesheets randomly when it is accessed.
And also, when the user visits and a random stylesheet is loaded, is it possible to remember which stylesheet was loaded, and only load that stylesheet for that particular visitor (i.e. stop randomising... load the style sheet which was originally loaded)?
I have jQuery loaded into html document.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script>
$(document).ready(function(){
var userStylesheet = $.cookie('user_stylesheet');
if(!userStylesheet){
userStylesheet = Math.floor((Math.random()*6)+1);
$.cookie('user_stylesheet', userStylesheet);
}
var ss = $('<link/>').attr('rel', 'stylesheet').attr('type','text/css').attr('href', 'stylesheet' + userStylesheet + '.css');
$(document.body).append(ss);
});
</script>
</head>
<body>
Hello!
</body>
</html>
EDIT
Here's the link for the jquery.cookie.js RAW
Do you have any kind of server-side scripting at your disposal? If so I would decide which CSS file to serve on the server, and save the value in a session variable—then if that session variable is set on subsequent page loads, output it instead of generating a new one.
Alternatively, you can try setting a cookie in javascript or local storage.
I am building a Firefox extension and have come a long way. One of the parts of my extension is where I display a DIV at the bottom of the page and I want to put an ad into the div, but for some reason every time it does so, it redirects to another page entirely with the ad on it. So it looks like its not allowing me to insert this code below into the div that sits at the bottom. Any chance anyone can help?
This code of course calls the "ad_display.js" file and thats where it does the redirect. Any way I can do this or am I stuck in the water?
Thanks!!!
<!-- Beginning of Project Wonderful ad code: -->
<!-- Ad box ID: 38154 -->
<script type="text/javascript">
<!--
var pw_d=document;
pw_d.projectwonderful_adbox_id = "38154";
pw_d.projectwonderful_adbox_type = "2";
pw_d.projectwonderful_foreground_color = "";
pw_d.projectwonderful_background_color = "";
//-->
</script>
<script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
<!-- End of Project Wonderful ad code. -->
Two solutions:
Either you replicate the functionality of that ad_display script, change "document.write(pw_s)" to something a little saner like "document.getElementById('mydivwithanad').innerHTML=pw_s"
Ether you use insert an iframe in the page rather than a div, and you inject that code in the iframe.