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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.