Paul
Paul

Reputation: 2515

Javascript function will not run

I have the following script:

<script>
    (function(d, s, id, p) {
var js, ins = d.getElementsByTagName(s)[0];
    js = d.createElement(s); 
    js.src = "http://www.site.com/feeds/run.php?id="+id+"&p="+p;
    ins.parentNode.insertBefore(js, ins);
data();
}(document, 'script', 131, 'large'));
</script>

The run.php actually returns a javascript which is running fine and executing a few functions, but the function I call in the above script (data();) will not execute. The data function reads:

function data(){
    var u;
    alert("I am an alert box");
    u = document.URL;
    document.body.innerHTML += '<form id="dynForm" target="transFrame" action="http://www.site.com/data.php" method="post"><input type="hidden" name="u" value="'+u+'"></form>';
    document.getElementById("dynForm").submit();
}

If anyone has information that may help me run this function it would be much appreciated.

-- Chris Shouts -- I have checked all the functions and they are fine. If I embed the script like:

<script type="text/javascript" src="http://www.site.com/feeds/run.php"></script>

But I can't use this line because I intend for people to add this to their websites and don't want them to remove these lines to stop certain operations. But when I do use this method I have to choose between running the other other functions in the file or this one.

Upvotes: 0

Views: 780

Answers (1)

Tyilo
Tyilo

Reputation: 30161

After inserting the script containing the data function, you don't wait for the script to finish loading, so the data function isn't available yet.

Try this:

(function(d, s, id, p) {
var js, ins = d.getElementsByTagName(s)[0];
    js = d.createElement(s); 
    js.src = "http://www.site.com/feeds/run.php?id="+id+"&p="+p;
    ins.parentNode.insertBefore(js, ins);
    var timer = setInterval(tryData, 100);
    function tryData()
    {
        if(window.data)
        {
            clearInterval(timer);
            data();
        }
    }
}(document, 'script', 131, 'large'));

Upvotes: 3

Related Questions