add javascript into a html page with jquery

后端 未结 3 1163
天涯浪人
天涯浪人 2020-12-03 23:55

I want to add a javascript google ad but I can\'t insert the javascript into the div using jquery. I try to simulate my problem with this test, which is using some advice I

3条回答
  •  心在旅途
    2020-12-04 00:50

    I figured out a great solution:

    1. Insert your Google Adsense code anywhere on your page - e.g. if your CMS only allows you to put this on the right hand side then stick it there.
    2. Wrap a div around it with display:none style
    3. Add some jquery code to move the div to the location you desire.

    Since the javascript has already run there is no problem then with moving the block of script to wherever you'd like it to be.

    e.g. if you wish to put 2 blocks of google adverts interspersed throughout your blog (say after paragraph 1 and after paragraph 4) then this is perfect.

    Here's some example code:

    
    
    
    

    p.s. #content happens to be where the content starts on my CMS (Squarespace) so you can replace that with whatever you have in your CMS. This works a treat and doesn't break Google ToS.

提交回复
热议问题