I’m trying to use jQuery to click a link in an iFrame if the URL contains specific text. The iFrame contains a twitter share link provided by addThis.

I think the iFrames are the issue but I can’t see how to get it to play ball.

My Code:

<script type="text/javascript">
$(function(){
    /*If the url contains*/
    if(window.location.href.indexOf("/test.html") > -1) {
            /*Wait for the iframe */
           $('#twitter-widget-0').ready(function () {
                /*Click link in .btn-o*/
                $(".btn-o").trigger("click");
            });
    }
});

Link to dropbox test site with addThis code: https://dl.dropboxusercontent.com/u/80852247/Demo/test.html#.UoNx9_m-2gL

I'm not getting any errors, I simply think that '.btn-o' isn't being detected because its loading within an iFrame.

Any work arounds?

有帮助吗?

解决方案 2

You can't use javascript to get things through an iFrame, that would be phishing (which is illegal) and is blocked by default by most browsers to prevent website scams.

You'll need to find another way to get that Share button onto your page.

其他提示

Try using the twitter api.

window.twttr = (function (d,s,id) {
  var t, js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
  js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
  return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));

twttr.widgets.createShareButton(
  'http://benward.me',
  document.getElementById('new-button'),
  function (el) {
    console.log("Button created.")
  },
  {
    count: 'none',
    text: 'Sharing a URL using the Tweet Button'
  }
);

twttr.ready(function (twttr) {
  // Now bind our custom intent events
  twttr.events.bind('click', clickEventToAnalytics);
  twttr.events.bind('tweet', tweetIntentToAnalytics);
  twttr.events.bind('retweet', retweetIntentToAnalytics);
  twttr.events.bind('favorite', favIntentToAnalytics);
  twttr.events.bind('follow', followIntentToAnalytics);
});

https://dev.twitter.com/docs/intents/events

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top