Here is a bit of code that will call the functions runAds() after the DOM has loaded. This is getting a bit advanced, but fun if you want to fiddle. Just be aware that IE will let you document.write stuff to the page at this point, but Firefox will start a new page if you want to document.write anything in. So if you are using this, you'll need to do it by adding iframes in to the source and load the ads in through there:
var alreadyrunflag=0 //flag to indicate whether target function has already been run
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; runAds()}, false)
else if (document.all && !window.opera){
document.write('\<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"\>\<\/script\>')
var contentloadtag=document.getElementById("contentloadtag")
contentloadtag.onreadystatechange=function(){
if (this.readyState=="complete"){
alreadyrunflag=1
runAds()
}
}
}
try{
var backupOnload = window.onload;
window.onload=function(){
if((typeof(backupOnload) != "undefined") &&(backupOnload != null)){backupOnload();}
setTimeout("if (!alreadyrunflag) runAds()", 0)
}
}catch(e){}
another one you might like to play with is the defer attribute for calling an external script:
document.write('\<SCR' + 'IPT SRC="' + allAds + '?" type="text/JavaScript" onLoad="runAds();" defer="defer" language="JavaScript"\>');
document.write('\</SCR' + 'IPT\>');
have fun - but remember that we're starting to push the boundaries of what we can do with our adcode here, so test in all browsers and different versions and then make sure you test with all sorts of ads including rich media
Wednesday, March 18, 2009
get query string value
Most people will be trying to get values from a query string by using the .split() javascript function. Basically the way to do this is to grab your string after the first question mark
string = string.substring(string.indexOf("?")+1);
then splitting on ampersand
values = string.split("&");
then going through each value checking for the left hand side and returning the right if it matches
for(i=0;i\<values.length;i++)
if((values[i].split("="))[0] == name)
try{return (values[i].split("="))[1]}catch(e){return ""};
return "";
I have also done it in the past just using string indexOfs:
//zMSmyStr is the query string, zMStagline is the variable name being searched for
function zMSgetValue(zMSmyStr,zMStagline)
{
zMSmyStr = zMSmyStr+"=";
var zMSind = zMStagline.indexOf(zMSmyStr)+zMSmyStr.length;
if(zMSind==(zMSmyStr.length-1)) //test for myStr not found
return "";
zMStagline += "&";
return zMStagline.substring(zMSind,zMStagline.indexOf("&",zMSind));
}
but I decided to save space to redo these as a regex and save time and space. So here it is:
function fd_fn_Query(myQS)
{
try{
return myURL.match(new RegExp("\[?&]"+myQS+"=([^&]*)"))[1];
}catch(e){return "";}
}
much smaller and easier to use, feel free to take this and modify to suit your needs.
string = string.substring(string.indexOf("?")+1);
then splitting on ampersand
values = string.split("&");
then going through each value checking for the left hand side and returning the right if it matches
for(i=0;i\<values.length;i++)
if((values[i].split("="))[0] == name)
try{return (values[i].split("="))[1]}catch(e){return ""};
return "";
I have also done it in the past just using string indexOfs:
//zMSmyStr is the query string, zMStagline is the variable name being searched for
function zMSgetValue(zMSmyStr,zMStagline)
{
zMSmyStr = zMSmyStr+"=";
var zMSind = zMStagline.indexOf(zMSmyStr)+zMSmyStr.length;
if(zMSind==(zMSmyStr.length-1)) //test for myStr not found
return "";
zMStagline += "&";
return zMStagline.substring(zMSind,zMStagline.indexOf("&",zMSind));
}
but I decided to save space to redo these as a regex and save time and space. So here it is:
function fd_fn_Query(myQS)
{
try{
return myURL.match(new RegExp("\[?&]"+myQS+"=([^&]*)"))[1];
}catch(e){return "";}
}
much smaller and easier to use, feel free to take this and modify to suit your needs.
Tuesday, March 17, 2009
Prototype.js and for(..in..) loops
I had a problem a while ago where I was using a for(..in..) loop to go through a hash map (like an array but has strings for the index instead of numbers). Now you NEED a for(..in..) loop for these, because you don't always know what the indexes are going to be called, and you can't call them sequentially like a normal array. Problem was that I was getting back what looked like javascript functions instead of the values I expected, so I did what any programmer would do after finding no easy explanation - I just checked for that function keyword, the length of the string etc, and skipped over the values that were not in a format that would work. Since then I have encountered the problem again, but luckily it was on a site where the developer knew his stuff:
http://www.prototypejs.org/api/array
it seems that the prototype.js javascript library absolutely hates the for(..in..) loop. So for these site you have to return the array as an enumerable type and then use the each function. If you are writing code to go across multiple sites, you may want to use a try catch block to check the existence of each going forward.
You have now been warned
**Edit**
I stumbled upon this easy fix:
var object = new Object();
for(var key in object){
if(!(key in Object.prototype)){
// do some stuff SAFELY!
}
}
http://www.prototypejs.org/api/array
it seems that the prototype.js javascript library absolutely hates the for(..in..) loop. So for these site you have to return the array as an enumerable type and then use the each function. If you are writing code to go across multiple sites, you may want to use a try catch block to check the existence of each going forward.
You have now been warned
**Edit**
I stumbled upon this easy fix:
var object = new Object();
for(var key in object){
if(!(key in Object.prototype)){
// do some stuff SAFELY!
}
}
Subscribe to:
Posts (Atom)