ReferenceError:从iframe调用时未定义函数

ReferenceError: function is not defined when calling from iframe

本文关键字:未定义 函数 调用 iframe ReferenceError      更新时间:2023-09-26

我正在尝试创建一个类似于thefancy.com网站的bookmarklet功能,我创建了一个bookmarklet按钮,当点击该按钮时,会加载位于其他网站上的js文件http://wonderstreet.localhost.com并将其附加在当前文档的头部分中,然后创建一个iframe并将其添加到当前文档的正文中。

上面提到的js文件包含了我需要从这个iframe调用的各种函数,例如,iframe中有一个"关闭"按钮,当点击它时,它应该调用位于上面js文件中的函数(这个函数将关闭或删除iframe)

将正确创建和显示iframe。(我需要获取当前文档的所有图像并将其显示在iframe中)问题是,当我单击关闭按钮时,它会显示:ReferenceError:函数未定义有人能帮我,让我知道我该怎么解决这个问题吗?[我使用的是核心javascript,而不是jquery]

这是js文件=>中的代码

function create_bookmarklet_iframe(thewonderstreet_userid) {
 var ifra=document.createElement('iframe');ifra.src="http://localhost.com/index.php/image_picker?userid="+userid;ifra.setAttribute("id","bookmarklet_iframe");ifra.setAttribute('allowtransparency',true);ifra.setAttribute('style','width:279px;height:372px;border:1px solid #4c515c;position:fixed;top:10px;right:10px;z-index:10000001;margin:0;background-color:#eff1f7;');void(document.body.appendChild(ifra));

}

function remove_bookmarklet_iframe(id) {
    var element = document.getElementById(id);
    element.parentNode.removeChild(element);
}

这是书签代码:

javascript:%20(function%20()%20{%20%20%20%20%20userid%20=%20'724c5a0e49e4dac588a90e17233982493027197d';%20%20%20%20%20var%20search_url%20=%20'http://localhost.com/javascript/bookmarklet_js.js';%20scripts_finder%20=%20document.getElementsByTagName('script');%20var%20found_url%20=%200;%20for%20(var%20i%20=%20scripts_finder.length;%20i--;)%20{%20var%20actual_url%20=%20scripts_finder[i].src.split("?");%20if%20(search_url%20==%20actual_url[0])%20{%20found_url++;%20}%20}%20if(found_url%20<=%200)%20{%20s=document.createElement('SCRIPT');s.type='text/javascript';s.src='http://localhost.com/javascript/bookmarklet_js.js?_='+(Math.random());document.getElementsByTagName('head')[0].appendChild(s);%20}%20%20var%20s_id%20=%20'bookmarklet_iframe',%20%20%20%20%20%20%20%20%20s_avail%20=%20document.getElementById(s_id),%20%20%20%20%20%20%20%20%20can_continue%20=%20true,%20%20%20%20%20%20%20%20%20t;%20%20%20%20%20if%20(s_avail)%20{%20%20%20%20%20%20%20%20%20can_continue%20=%20false;%20%20%20%20%20%20%20%20%20alert('Alreadyopen');%20%20%20%20%20}%20%20%20%20%20setTimeout(function()%20{%20if%20(can_continue)%20{%20create_bookmarklet_iframe(userid);%20}%20},1000);%20})();

好的,我自己解决了,我做的是错误的,我没有创建一个iframe,而是使用了div,所以基本上我删除了iframe、用javascript创建了div并将其附加到文档正文中。