如何执行内的代码只有当我展示它的容器时?
我有 this 代码:
HTML
<a href="javascript:void(0);" id="showDiv">Show Agency</a>
<div id="invisibleDiv">
<script src="http://platform.linkedin.com/in.js" type="text/javascript"></script>
<script type="IN/CompanyProfile" data-id="1035" data-format="inline" data-related="false"></script>
</div>
jQuery
$("#showDiv").click(function () {
$("#invisibleDiv").show();
});
CSS
#invisibleDiv
{
display:none;
}
当我加载页面时,我看到从外部加载的脚本源,如果 div 是隐藏的。脚本调用一些 API 并生成 HTML/Javascript。
好吧,我正在寻找的是如果父级被隐藏则强制卸载脚本;然后,当我显示它(通过链接)时,将脚本加载到 div 内。
我该怎么做呢?我会避免 AJAX。
I have this code :
HTML
<a href="javascript:void(0);" id="showDiv">Show Agency</a>
<div id="invisibleDiv">
<script src="http://platform.linkedin.com/in.js" type="text/javascript"></script>
<script type="IN/CompanyProfile" data-id="1035" data-format="inline" data-related="false"></script>
</div>
jQuery
$("#showDiv").click(function () {
$("#invisibleDiv").show();
});
CSS
#invisibleDiv
{
display:none;
}
When I load the page, I see the scripts loaded from external source, also if the div is hidden. The scripts call with some API and generate HTML/Javascript.
Well, what I'm looking for is to force the script to be unloaded if the parent is hidden; than, when I'll show it (through the link), load the script inside the div.
How can I do it? I'll avoid AJAX.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新:
UPDATED:
如果我理解正确的话,您只是想延迟第三方提供的任意脚本的运行?像这样的东西会起作用吗?
缺点是无法预取 HTML 内容,但我没有看到任何其他方法。
编辑:好的,看来您在我输入此内容时编辑了问题...所以您可以控制invisibleDiv的内容,但您想延迟in.js的加载?试试这个...
同样,您必须让用户在脚本下载时等待,因此我添加了 pleaseWaitDiv
更多编辑:
新建一个脚本节点并附加它。
If I understand it correctly, you just want to delay the running of arbitrary scripts that are provided by the third party? Would something like this work?
Has the downside that you can't pre-fetch the HTML content, but I don't see any other way.
EDIT: ok, it looks like you edited the question whle I was typing this up... so YOU control the content of invisibleDiv, but you want to delay the loading of in.js? try this...
Again, you have to make the user wait while the script downloads, hence my addition of pleaseWaitDiv
More edit:
New up a script node and append it.
您可以将它绑定到您的
showDiv
点击函数,内联脚本会立即处理...我唯一能想到的就是通过 ajax 加载脚本,这是您不想要的。You can bind it to your
showDiv
click function, inline scripts are processed right away...the only other thing I can think of is to have the script be loaded via ajax which you don't want.试试这个:
http://jsfiddle.net/sXJa6/6/
Try this :
http://jsfiddle.net/sXJa6/6/