第二个外部 javascript 文件未加载
我的 html head 部分中有这些行当
<script type="text/javascript" src="../behaviour/location.js"></script>
<script type="text/javascript" src="../behaviour/ajax.js"></script>
我单独使用其中任何一个时,外部文件中的代码将按预期执行。
然而,当我同时使用两者时,我发现两者都无法正常工作。我需要做什么来解决这个问题?
location.js
// JavaScript Document
function addLoadEvent (func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldonload();
func;
}
}
}
//county changer
function countyUpdate (message) {
var name = message.getAttribute("name");
var check = message.checked;
var countyId = message.getAttribute("id");
var countyId = countyId.split("_")[1];
var innerpost = document.getElementById("innerpost_"+countyId);
var checks = innerpost.getElementsByTagName("input");
for (var i = 0; i< checks.length; i++) {
if (checks[i].checked == true && check == true) {
checks[i].checked = false;
}
}
}
//postcode changer
function postcodeUpdate (message) {
var parent = message.parentNode.parentNode.getAttribute("id").split("_")[1];
var county = "county_"+parent;
var checkbox = document.getElementById(county);
var checked = message.checked;
if (checked == true) {
checkbox.checked = false;
}
}
//get a dynamic list of al postcode checkboxes
function getCounties () {
var county = document.getElementsByTagName("input");
for (var i = 0; i< county.length; i++) {
var check = county[i].getAttribute("type");
if (check == "checkbox") {
var name = county[i].getAttribute("name");
var parent = county[i].parentNode.parentNode.getAttribute("id");
var parent = parent.split("_")[0];
//action for county
if (parent != "innerpost") {
county[i].onclick = function() { countyUpdate(this); };
}//if
//action for postcode
if (parent == "innerpost") {
county[i].onclick = function() { postcodeUpdate (this); };
}//if
}//if
}//for
}//function
addLoadEvent (getCounties);
ajax.js
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","cart.php",true);
xmlhttp.send();
}
这是激活第二个文件的内联代码:
<button type="button" onclick="loadXMLDoc()">Change Content</button>
当我尝试一起使用这两个文件时,我似乎无法使用任何函数(甚至不能使用函数中包含的简单警报)。
I have these lines in my html head section
<script type="text/javascript" src="../behaviour/location.js"></script>
<script type="text/javascript" src="../behaviour/ajax.js"></script>
When I use either in isolation, the code in the external files executes as expected.
However, when I use both, I find that neither works correctly. What do I need to do to fix this?
location.js
// JavaScript Document
function addLoadEvent (func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldonload();
func;
}
}
}
//county changer
function countyUpdate (message) {
var name = message.getAttribute("name");
var check = message.checked;
var countyId = message.getAttribute("id");
var countyId = countyId.split("_")[1];
var innerpost = document.getElementById("innerpost_"+countyId);
var checks = innerpost.getElementsByTagName("input");
for (var i = 0; i< checks.length; i++) {
if (checks[i].checked == true && check == true) {
checks[i].checked = false;
}
}
}
//postcode changer
function postcodeUpdate (message) {
var parent = message.parentNode.parentNode.getAttribute("id").split("_")[1];
var county = "county_"+parent;
var checkbox = document.getElementById(county);
var checked = message.checked;
if (checked == true) {
checkbox.checked = false;
}
}
//get a dynamic list of al postcode checkboxes
function getCounties () {
var county = document.getElementsByTagName("input");
for (var i = 0; i< county.length; i++) {
var check = county[i].getAttribute("type");
if (check == "checkbox") {
var name = county[i].getAttribute("name");
var parent = county[i].parentNode.parentNode.getAttribute("id");
var parent = parent.split("_")[0];
//action for county
if (parent != "innerpost") {
county[i].onclick = function() { countyUpdate(this); };
}//if
//action for postcode
if (parent == "innerpost") {
county[i].onclick = function() { postcodeUpdate (this); };
}//if
}//if
}//for
}//function
addLoadEvent (getCounties);
ajax.js
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","cart.php",true);
xmlhttp.send();
}
And this is the inline code to activate the second file:
<button type="button" onclick="loadXMLDoc()">Change Content</button>
When I try using both files together, I don't seem to be able to use any functions (not even simple alerts wrapped in a function).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果脚本相互冲突就会发生这种情况。
请向我们展示脚本和错误消息。
This would happen if the scripts conflict with each other.
Please show us the scripts and the error messages.
听起来这两个文件中的某个地方有冲突。就像一个具有相同名称的函数或一个变量等。
在没有看到这些文件的情况下,您可以:
1. 找出命名冲突(如果确实如此)并进行更改。
2. 将文件中的代码封装在不同的对象中,并通过那里访问方法/属性等。
It sounds like you have a conflict somewhere in the two files. Like a function that is named the same or a variable etc.
Having not seen the files you could:
1. Track down the naming conflict (if that is what it is) and change one.
2. Encapsulate the code in the files in different objects and access the methods/properties etc. through there.
我敢打赌,您会因为
window.onload=
的杂耍而变得一团糟。我建议您使用标准window.addEventListener()
接口(如果您想支持 IE,请检查其是否存在并在缺少标准接口时使用window.attachEvent()
也)。这还应该保证 onload 函数按照与设置它们的脚本相同的顺序执行。像这样的东西:My bet is that you're getting a mess out of that
window.onload=
juggling. I suggest you use the standardwindow.addEventListener()
interface (check for its existance and usewindow.attachEvent()
when the standard interface is missing if you want to support IE too). This should also guarantee that the onload functions get executed in the same order as the scripts that set them. Something like: