简体   繁体   中英

Javascript function returns variables as undefined

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);

Where are you passing the parameters?

Try this:

timer = setInterval(function(){
     tryData(131, 'large');
}, 100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM