0

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);
7
  • You are calling it just like that, with hardcoded parameters? Commented Feb 2, 2012 at 20:05
  • what about using some kind of debugger ( each browser have one ) Commented Feb 2, 2012 at 20:05
  • garbage in = garbage out. What parameters are being passed? Where do they come from? Commented Feb 2, 2012 at 20:07
  • Try debugging with Firebug and post more detailed examples regarding the HTML-page and JavaScript. Commented Feb 2, 2012 at 20:07
  • 1
    Sounds like a hoisting problem adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting Commented Feb 2, 2012 at 20:08

1 Answer 1

4

Where are you passing the parameters?

Try this:

timer = setInterval(function(){
     tryData(131, 'large');
}, 100);
Sign up to request clarification or add additional context in comments.

2 Comments

I tired this but it just seems to continually reload the page. I will play with it and see if I can get it to stick. Thanks
Just need to remove the "timer = setInterval(tryData, 100);" from my js file for this to work for me. Many thanks Alex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.