Conflict between 2 scripts

0

When I put the second script the first one disappears is the first times 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 are the same, so I changed the Req to Req1.

    
asked by anonymous 16.05.2016 / 17:15

1 answer

0

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

Try changing the name of your functions.

    
16.05.2016 / 17:19