Javascript 从localStorage获取值并附加到iframe URL
我试图从Javascript 从localStorage获取值并附加到iframe URL,javascript,jquery,html,iframe,local-storage,Javascript,Jquery,Html,Iframe,Local Storage,我试图从localStorage中检索一个值,并将其附加到iframe URL中,然后将其写入页面 我试图用文档呈现iframe。编写,但我认为我做得不对 以下是我目前掌握的情况: <script> jQuery(document).ready(function($) { $( ".postid-192" ).load(function() { var email = localStorage.getItem("email");
localStorage
中检索一个值,并将其附加到iframe URL中,然后将其写入页面
我试图用文档呈现iframe。编写,但我认为我做得不对
以下是我目前掌握的情况:
<script>
jQuery(document).ready(function($) {
$( ".postid-192" ).load(function() {
var email = localStorage.getItem("email");
document.write('<iframe src="http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email + ' width="1" height="1"></iframe>');
});
});
</script>
jQuery(文档).ready(函数($){
$(“.postid-192”).load(函数(){
var email=localStorage.getItem(“电子邮件”);
文件。写('
write()方法主要用于测试:如果在HTML文档完全加载后使用,它将删除所有现有的HTML
因此,不要使用文档。请编写。此代码是正确的:
<script>
$(document).ready(function() {
$(".postid-192").load(function() {
var email = localStorage.getItem("email");
$("#iframe").html('<iframe src="http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email + '" width="1" height="1"></iframe>');
});
});
</script>
$(文档).ready(函数(){
$(“.postid-192”).load(函数(){
var email=localStorage.getItem(“电子邮件”);
$(“#iframe”).html(“”);
});
});
和HTML:
<div id="iframe"></div>
write()方法主要用于测试:如果在HTML文档完全加载后使用,它将删除所有现有的HTML
因此,不要使用文档。请编写。此代码是正确的:
<script>
$(document).ready(function() {
$(".postid-192").load(function() {
var email = localStorage.getItem("email");
$("#iframe").html('<iframe src="http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email + '" width="1" height="1"></iframe>');
});
});
</script>
$(文档).ready(函数(){
$(“.postid-192”).load(函数(){
var email=localStorage.getItem(“电子邮件”);
$(“#iframe”).html(“”);
});
});
和HTML:
<div id="iframe"></div>
附加iframe的正确方法:
var i=document.createElement('iframe');
i.src= 'http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email;
i.width=1;
i.height=1;
document.body.appendChild(i);
附加iframe的正确方法:
var i=document.createElement('iframe');
i.src= 'http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email;
i.width=1;
i.height=1;
document.body.appendChild(i);
您也没有关闭src属性的双引号。@DavidMoškoř-这是要将src附加到iframe吗?等等……NM……我看到了now@DavidMoškoř-是的!我必须更新一个小东西,但它现在正在做它应该做的。谢谢!你也没有关闭src属性的双引号。@DavidMoškoř-是这样吗应该把src附加到iframe?等等…NM…我看到了now@DavidMoškoř-Yep!我必须更新一个小东西,但它现在正在做它应该做的事情。谢谢!永远不要使用document.write()
,除非绝对没有其他选择。尝试document.body.appendChild()
。还要注意load()
作为事件处理程序已被弃用。除非绝对没有其他选择,否则切勿使用document.write()
。请尝试使用document.body.appendChild()
作为事件处理程序。另外,请注意load()
已被弃用。