如何在页面加载后注入具有document.write的第三方脚本

时间:2017-12-14 17:26:50

标签: javascript jquery ajax

所以基本上这段代码应该根据用户的位置注入一个脚本。

$.ajax("http://freegeoip.net/json/"+ip)

.done(function(data)

{
  if(data.country_name == "India")
  {

var geodata = '<script type="text/javascript">amzn_assoc_ad_type ="responsive_search_widget"; amzn_assoc_tracking_id ="murkind-21"; amzn_assoc_marketplace ="amazon"; amzn_assoc_region ="IN"; amzn_assoc_placement =""; amzn_assoc_search_type = "search_widget";amzn_assoc_width ="auto"; amzn_assoc_height ="auto"; amzn_assoc_default_search_category =""; amzn_assoc_default_search_key ="";amzn_assoc_theme ="light"; amzn_assoc_bg_color ="FFFFFF"; <\/script><script src="//z-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&Operation=GetScript&ID=OneJS&WS=1&Marketplace=IN"><\/script>';
$("#fdsa").html(geodata);
}


}

    );

上面的代码给出了以下错误:

  
      
  1. “无法在'Document'上执行'write':除非明确打开,否则无法从异步加载的外部脚本写入文档。”
  2.   
  3. GET https://ws-na.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&MarketPlace=IN&Operation=GetAdHtml&OneJS=1&slotNum=2&height=auto&placement=adunit&theme=light&tracking_id=“”&amp; bg_c​​olor = FFFFFF&amp; width = auto&amp; ad_type = responsive_search_widget&amp; marketplace = amazon&amp; search_type = search_widget&amp; region = IN&amp; default_search_category =&amp; default_search_key =“”&amp; viewerCountry = IN&amp; amp ; jsonp = amzn_assoc_jsonp_callback_adunit_2 net :: ERR_ABORTED
  4.   

所以经过一些谷歌搜索后,我学会了一些名为postscribe的东西并试了一下。

postscribe('#fdsa', '<script type="text/javascript">amzn_assoc_ad_type ="responsive_search_widget"; amzn_assoc_tracking_id =""; amzn_assoc_marketplace ="amazon"; amzn_assoc_region ="IN"; amzn_assoc_placement =""; amzn_assoc_search_type = "search_widget";amzn_assoc_width ="auto"; amzn_assoc_height ="auto"; amzn_assoc_default_search_category =""; amzn_assoc_default_search_key ="";amzn_assoc_theme ="light"; amzn_assoc_bg_color ="FFFFFF"; <\/script><script src="//z-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&Operation=GetScript&ID=OneJS&WS=1&Marketplace=IN"><\/script>');

使用postscribe,我不再得到文档写入错误但我仍然得到GET错误。

0 个答案:

没有答案