Loading

This is my first time posting an IT solution. Hopefully someone will google this and find it helpful. It's really a simple solution but I wished someone posted this and save me 2 days of headache. For a while I had a dillema on how to show ad on a 100% ajax web site. It's totally unfair to show only 1 impression of ads while the user is browsing your site. Currently, I'm using Google's Adsense and they don't give any help on Ajax Web thus I had to come w/ my own solution. Of course there was some help on the web to do this but it was a combination of results + my own solution that made it work.

Ok~ so let see what is fair use of this adsense. In my opinion (yours may differ..even Google), if there's a content change in your web site then it's fair to show a new ads on your page. Since I'm using AJAX, the best place to refresh the ad would be where you're making your HTTP request to the server. Example!

var oXmlHttp = createXMLHttp();

oXmlHttp.onreadystatechange = function() { 
     if (oXmlHttp.readyState == 4) {
          var storyDiv = document.getElementById(layerName); 
          storyDiv.innerHTML = oXmlHttp.responseText; 

           refreshAd(); 
      }
};

refreshAd() will refreash the ad. No better place then here. Do not put this under any onClick/onChange/onAnything behavior as it will only complicate your page like onClick="doSomething();refreshAd();" 

// Need to pass unique URI each time to make sure that your browser does not cache the result! This is needed for Firefox specifically!
function refreshAd() {
      var currentTime = new Date(); 
      googlead.location = "pages/ad.jsp?s=" + currentTime.getTime(); 
}

That's right! I'm using IFRAME.. I can already see you're not surprised as if you already know the solution. There's really no other option since any other solutions will be breaking Google's TOS. 
<iframe id="googlead" name="googlead" href="" src="" width="100%" height="100" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"/> 

Create a globally access IFRAME named "googlead" or whatever you desired. To make sure that this page does not get cache, always pass a unique parameter. In this case I used client's system time. You could use other algorithm to create unique random characters but for lazy people like me will use what I did.

Great! I got my IFRAME to refresh on every HTTP/AJAX request.. but what the hell! it only shows Public Service Ad. I want to show ads that's related to my site!
Now in order for Google to content search your page... the content has to be inside wherever you put your google adsense script. For now this is what I had.

--ad.jsp-- 
<script type="text/javascript">
<!-- 
google_ad_client = "googleID";
google_ad_width = 728; 
google_ad_height = 90; 
google_ad_format = "728x90_as"; 
google_ad_type = "text"; 
google_ad_channel =""; //-->
</script> 
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 


No wonder I'm getting PSA, there's no content!! now if I put a content then it shows litterally on your page.. which becomes stupid. Showing Ad and showing key word contents to the user will quickly condemn your site to hell. Now how can I put the content that's not visible to the audience? By pure stupid dumb luck, I found out that you can write invisible contents in your IFRAME by setting absolute height (that's right it took 2 days..my other solutions were too complicated and all failed).
<iframe id="googlead" name="googlead" href="" src="" width="100%" height="100" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"/> 
For my ad I only need height of 100, so if I write anything below then it's invisible to the user and visible to the google content search engine! beautiful!! I just love dumb simple solution!

Ok~ now I put
<script type="text/javascript">
<!-- 
google_ad_client = "googleID";
google_ad_width = 728; 
google_ad_height = 90; 
google_ad_format = "728x90_as"; 
google_ad_type = "text"; 
google_ad_channel =""; //-->
</script> 
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 
<br/><br/><br/><br/> 
<% if (yourKeyWord != null) { 
           print your key words here!
      } else { 
          print your general key words in case you don't have any. Example "Vacation Money Dating" better than PSA's!      
} %> 


Voila! Now I have total control of the content key words for google engine! In this case I used JSP where I save the content in my session and display on this page. But, I'm sure this techinque will work on whatever programming language.

Category: ajax

Comment and make your thoughts visible

Window.onload() is a workhorse of traditional javascript code. It’s been used by coders for years to kick start the client side magic as soon as everything on the page loads up.

But sometimes waiting for a page to load just isn’t quick enough.

A few large image files will quickly reveal that window.onload() can be painfully slow. So when I was creating a web app for internet marketers recently, I had to have something faster.

Some quick research into possible workarounds for the window.onload() issue brought me to some code by Brother Cake. If all you need is a fast way to kick start your javascript then their code might be something to try.

But if you’re going to be doing some DOM (Document Object Model) javascript coding then why not use jQuery and have your cake and eat it too (horrible pun - sorry).

jQuery has a handy little function that launches your javascript as soon as the Document Object Model is ready… which happens before the page has finished loading.

$(document).ready(function(){
  // Your code here...
});

You can use it to launch any kind of javascript you like. It doesn’t have to be reserved for jQuery style coding. And there’s nothing wrong with telling jQuery to launch several different functions at once.

Similar to many init() functions you may have seen before… just a lot faster.

You’ll see this code used again and again in the examples I give you on 15 Days of jQuery.

Category: jquery

Comment and make your thoughts visible

I had to write some good old classic ASP code today and my classic ASP coding skills are so rusty that I put tons of errors in the code. I'm used to having basic ASP error messages on IIS5 and 6 which usually help me to track down problems. On IIS7 I only got the following though:

An error occurred on the server when processing the URL. Please contact the system administrator

After investigating a bit I figured out that we changed the default for the "scriptErrorSentToBrowser" flag in IIS7. It's now false and you always get the error above. Here is how to change it:

1) Start an elevated command prompt. Right-click the command shell item in the Start-Accessories menu and select "Run as Administrator".

2) Run the following command: %windir%\system32\inetsrv\appcmd set config -section:asp -scriptErrorSentToBrowser:true

Once you are done with debugging your ASP app please set it back to false. There are lots of 'evildoers' out there! :)

Category: aspsecurity

Comment and make your thoughts visible

One problem that often tricks me in ASP.net is the presence of null values. This article describes how to deal with null strings and null database values.

Null strings

It is not immediately obvious that a null string is not our old friend "". You would think that if a string contained nothing, it would be null, but it isn't. A null string is a string that hasn't been assigned a value, but an empty string is a string that has been assigned an empty value.

You can check if a string is null by using this:

if (myString == null) { do this or that; }


If you try to find the length of a null string, you can get this rather terse error message:

Object reference not set to an instance of an object.


So, it is best to check that the string isn't null first, then try to find its length. You may end up with a null string if you tried to assign a string to a value in a collection, and the value was not present in the collection. You're unlikely to set a string to null yourself, and due to the fact that they are confusing to work with, I don't recommend trying it.

Null Database Values

You can use the useful IsDBNull method to find out if something you have read from a database is a null value. This is an important thing to do, because often you will assume that a field called "bank_balance" in a database, that is a numeric type, must hold a numeric value. But you'd be wrong, because maybe it holds a null value. So, whenever I am reading from a database, I check if the value is null before getting it:


SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
   if (!reader.IsDBNull(1))
   {
      bool title_only = reader.GetBoolean(1);
   }
}


(For more information about SQL, see my 
SQL Server page.)

In Visual Basic you can do this:


If reader3.Item(2) IsNot DBNull.Value Then hours += reader3.Item(2)


Conclusion

Null can trick you in many ways, so always think "could this value ever contain null?" and if so, take precautions.

Category: .netc#vbasp.net

Comment and make your thoughts visible

CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files… ‘Access is denied.’
Do you face this problem lately? I did. I have problem to run ASP.NET web application on a fresh installed server. It cannot run ASP.NET even I install .NET Framework 3.5. Web application can be loaded after I register ASP.NET into IIS by using “aspnet_regiis.exe -i -enable“.

However, I faced the problem statement above. So what is the solution? It have something to do with file permission. Just follow below steps to fix it.
Grant full control to two users of your system “Network Service” and “YourComputerName\IIS_IUSERS” on the following folders.
1. C:\Windows\Temp
2. C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
Restart IIS and try run your web application again.

Hope this guide will help.

Category: asp.net

Comment and make your thoughts visible

« NextPage 2 of 2  
MoneyBookers