加载后设置变量

时间:2017-04-14 23:54:22

标签: javascript jquery html onload

一旦我的页面加载通过第三方应用程序生成了一个唯一的链接,我需要存储在变量中以便稍后访问。

如何将链接设置为变量 AND 如何确保在页面加载后完成?

第三方应用程序生成的代码:

<div class="_ohe lfloat"><a href="THE LINK TO BE SET GENERATES HERE" src=".jpg" target="_blank" class="img _8o _8s UFIImageBlockImage"><img class="_1ci img" src="https://scontent.ftpa1-1.fna.fbcdn.net/v/t1.0-1/p48x48/12472831_1760775617477649_5525707532693192482_n.jpg?oh=32736fd5787e04e6f55aa8eb7ecaa529&amp;oe=59539C06" alt=""></a></div>

1 个答案:

答案 0 :(得分:1)

在页面加载后在链接中存储链接的href

编辑:添加一个间隔以获取值,如果在加载后稍微附加了该值。

$(document).ready(function(){

  var myHref = "";

  // Set an interval to check cyclically for the presence of href
  var waitForTheHref = setInterval(function(){

    // Look for the value
    myHref = $(document).find("._ohe.lfloat a").attr("href");

    // Check if the value is present
    if( myHref !== "" && myHref !== "undefined"){

      // Show it in console
      console.log(myHref);

      // You have the value... Clear the interval!
      clearInterval(waitForTheHref);
    }

  },50);  // milliseconds.
});