Javascript 函数返回未定义的变量
我动态地将 javascript 文件包含到 html 页面中,效果很好。但是当我从文件中运行函数“tryData”时,变量返回为未定义。我找了好几个小时都找不到类似的问题,有人知道问题是什么吗?
外部文件中的函数:
function tryData(id, size){
document.getElementById('content').innerHTML = '<iframe src="http://domain.com/feeds/'+id+'/'+size+'/" id="frame"></iframe>';
if(window.data){
clearInterval(timer);
data();
}
}
我用来调用它的行:
tryData(131, 'large');
我知道该函数正在运行,因为框架已按预期插入,但没有内容,因为框架的 URL 为“domain.com/feeds/undefined/undefined” /',而不是'domain.com/feeds/131/large/'。
预先感谢您的帮助:)
这是一个带有该函数的 html 页面的示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="robots" content="NOINDEX,NOFOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<!-- Widget -->
<div id="content"></div>
<script>
(function(d, s) {
var js = d.createElement(s), ins = d.getElementsByTagName(s)[0];
js.src = "http://domain.com/feeds/insert.js";
ins.parentNode.insertBefore(js, ins);
tryData(131, 'large');
}(document, 'script'));
</script>
<!-- Widget End -->
</body>
</html>
这里是 js 文件:
function data(){
var u;
u = document.URL;
$.post("http://domain.com/data.php", { u: u} );
}
function tryData(id, size){
document.getElementById('lsb').innerHTML = '<iframe src="http://domain.com/feeds/'+id+'/'+size+'/" id="frame"></iframe>';
if(window.data){
clearInterval(timer);
data();
}
}
var script = document.createElement('script');
script.src = 'http://code.jquery.com/jquery-latest.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
timer = setInterval(tryData, 100);
I am dynamically including a javascript file into a html page, which works fine. But when I run the function 'tryData' from the file the variables return as undefined. I've been looking for hours can't find a similar problem anywhere, does anyone know what the problem is?
function in external file:
function tryData(id, size){
document.getElementById('content').innerHTML = '<iframe src="http://domain.com/feeds/'+id+'/'+size+'/" id="frame"></iframe>';
if(window.data){
clearInterval(timer);
data();
}
}
the line I am using to call it:
tryData(131, 'large');
I know that the function is running because the frame is inserted as expected, but there's no content as the URL for the frame reads 'domain.com/feeds/undefined/undefined/', instead of 'domain.com/feeds/131/large/'.
Thanks in advance for any help :)
Here is an example of a html page with the function:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="robots" content="NOINDEX,NOFOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<!-- Widget -->
<div id="content"></div>
<script>
(function(d, s) {
var js = d.createElement(s), ins = d.getElementsByTagName(s)[0];
js.src = "http://domain.com/feeds/insert.js";
ins.parentNode.insertBefore(js, ins);
tryData(131, 'large');
}(document, 'script'));
</script>
<!-- Widget End -->
</body>
</html>
here is the js file:
function data(){
var u;
u = document.URL;
$.post("http://domain.com/data.php", { u: u} );
}
function tryData(id, size){
document.getElementById('lsb').innerHTML = '<iframe src="http://domain.com/feeds/'+id+'/'+size+'/" id="frame"></iframe>';
if(window.data){
clearInterval(timer);
data();
}
}
var script = document.createElement('script');
script.src = 'http://code.jquery.com/jquery-latest.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
timer = setInterval(tryData, 100);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在哪里传递参数?
试试这个:
Where are you passing the parameters?
Try this: