Conflict between 2 scripts

Asked

Viewed 52 times

0

When I put the second script the first script disappears is the first time to use ajax.

I call the scripts like this:

<script language="javascript" type="text/javascript" src="script.js">
</script>


<div id="relogio"></div>





<script language="javascript" type="text/javascript" src="proparagem.js">
</script>

<div id="proparagem"></div>

</div>

The script.js:

  var req;

function loadXMLDoc(url)
{
    req = null;

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);

    } else if (window.ActiveXObject) {
        try {
req = new ActiveXObject("Msxml2.XMLHTTP.4.0");
} catch(e) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch(e) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP"); 
} catch(e) {
req = false;
}
}
}
}
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange()
{

    if (req.readyState == 4) {

        if (req.status == 200) {

            document.getElementById('relogio').innerHTML = req.responseText;
        } else {

        }
    }
}

function buscarTempo()
{
    loadXMLDoc("relogio.php");
}


setInterval("buscarTempo()", 1000); 

The proparagem.js

var req1;

function loadXMLDoc(url2)
{
    req1 = null;

    if (window.XMLHttpRequest) {
        req1 = new XMLHttpRequest();
        req1.onreadystatechange = processReq1Change;
        req1.open("GET", url2, true);
        req1.send(null);

    } else if (window.ActiveXObject) {
        try {
req1 = new ActiveXObject("Msxml2.XMLHTTP.4.0"); 
} catch(e) {
try {
req1 = new ActiveXObject("Msxml2.XMLHTTP.3.0"); 
} catch(e) {
try {
req1 = new ActiveXObject("Msxml2.XMLHTTP"); 
} catch(e) {
try {
req1 = new ActiveXObject("Microsoft.XMLHTTP"); 
} catch(e) {
req1 = false;
}


}
}
}
        if (req1) {
            req1.onreadystatechange = processReq1Change;
            req1.open("GET", url2, true);
            req1.send();
        }
    }
}

function processReq1Change()
{

    if (req1.readyState == 4) {

        if (req1.status == 200) {

            document.getElementById('proparagem').innerHTML = req1.responseText;
        } else {

        }
    }
}

function buscarTempo()
{
    loadXMLDoc("compararhorarios.php");
}


setInterval("buscarTempo()",  30000); 

They’re the same I just changed Req to Req1 .

1 answer

0


James, what is happening is: your function loadXMLDoc and buscarTempo are in the global scope and with the same name in both archives. So when the second script is loaded, it simply overwrites the function that was in its first script.

Try to change the name of your functions.

  • That’s exactly what I had to do

Browser other questions tagged

You are not signed in. Login or sign up in order to post.