Jump to content

Inserting a script using innerHTML property


dpacmittal

Recommended Posts

It works for me. The script gets injected into the div element just fine. However it wont alert hey. That's because of the way the browser works. It processes all the html/javascript/css it's given once and there it stops (unless there is some javascript function that gets executed periodically but that's not a solution for you). It doesn't reprocess all the new html elements it's given. Why would you want that anyway? It doesn't seem to server any purpose.

Does the adsense code need to be in the div, or only the adsense content?

Only the adsense content needs to be in the div.

 

Okay...why can't you do something like:

 

<html>
<head>
   <script type="text/javascript">
      window.onload = function(){
         document.getElementById('test').innerHTML = '<a href="#" id="blah">Clicky<\/a>';

         document.getElementById('blah').onclick = function(){
            alert("Clicked");
         }
      }
   </script>
</head>

<body>
   <div id="test"></div>
</body>

</html>

 

It's a simple example, but it shows how you can inject markup into a preexisting element and tie an event handler to it.

Does the adsense code need to be in the div, or only the adsense content?

Only the adsense content needs to be in the div.

 

Okay...why can't you do something like:

 

<html>
<head>
   <script type="text/javascript">
      window.onload = function(){
         document.getElementById('test').innerHTML = '<a href="#" id="blah">Clicky<\/a>';

         document.getElementById('blah').onclick = function(){
            alert("Clicked");
         }
      }
   </script>
</head>

<body>
   <div id="test"></div>
</body>

</html>

 

It's a simple example, but it shows how you can inject markup into a preexisting element and tie an event handler to it.

But how can I display the adsense content inside that particular div?

This is an example of Google Adsense code:

 

<script type="text/javascript">

google_ad_client = "pub-1752524390xxxxx"

/* 728x90, created 6/6/09 */

google_ad_slot = "227288xxxx";

google_ad_width = 728;

google_ad_height = 90;

</script>

<script type="text/javascript" src="http://pagead2.googlesyndication.com/page/show_ads.js">alert("works");

</script>

 

 

Is there any other content?  I can't see where a clickable ad is being created.  The first script defines some properties, and the second links to a Google script that should allow ads to be displayed.  How are adsense ads normally displayed?  Wouldn't there need to be a target element of some sort that would actually display the ads?

 

Is there a tutorial somewhere?  Because I've never used adsense myself.

Is there any other content?  I can't see where a clickable ad is being created.  The first script defines some properties, and the second links to a Google script that should allow ads to be displayed.  How are adsense ads normally displayed?  Wouldn't there need to be a target element of some sort that would actually display the ads?

 

Is there a tutorial somewhere?  Because I've never used adsense myself.

Well, the script automatically creates a hiddin div in which ads are shown. As for tutorials, you can find tons of them by googling.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.