dpacmittal Posted July 29, 2009 Share Posted July 29, 2009 Why does this not work: <html> <head> </head> <body> <div id="test"></div> <script> document.getElementById("test").innerHTML = '<script>alert("hey");<\/script>'; </script> </body> </html> It should work, isn't it? Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/ Share on other sites More sharing options...
RichardRotterdam Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885742 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 Well, I am modifying the Shadowbox plugin for Wordpress to display adsense ads in it. For this purpose, I need to add Adsense Code inside the Div. I can't find any other place to insert it. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885750 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885798 Share on other sites More sharing options...
KevinM1 Posted July 29, 2009 Share Posted July 29, 2009 Does the adsense code need to be in the div, or only the adsense content? Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885821 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885823 Share on other sites More sharing options...
KevinM1 Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885836 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885875 Share on other sites More sharing options...
KevinM1 Posted July 29, 2009 Share Posted July 29, 2009 Can you give me an example of the content? Because I can't see why you couldn't just inject it with innerHTML. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885905 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 Well, I know injecting with innerHTML, but I want to insert adsense Ads using innerHTML. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885925 Share on other sites More sharing options...
RichardRotterdam Posted July 29, 2009 Share Posted July 29, 2009 Do you perhaps have this page online? It's practically impossible to help you without seeing what this adsense code looks like. That or paste some sample code like Nightslyr already suggested. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885926 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-885938 Share on other sites More sharing options...
KevinM1 Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-886003 Share on other sites More sharing options...
dpacmittal Posted July 29, 2009 Author Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-886024 Share on other sites More sharing options...
KevinM1 Posted July 29, 2009 Share Posted July 29, 2009 Okay...hmm...do you even need to do it dynamically? Is there anything in the Wordpress plugin that's forcing you to attempt injecting the adsense code? Quote Link to comment https://forums.phpfreaks.com/topic/167938-inserting-a-script-using-innerhtml-property/#findComment-886033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.