[英]Why doesn't this Javascript work with Opera or Chrome?
Thanks for reading. 谢谢阅读。
I have several scripts that are built similar to the following: 我有几个类似于以下内容的脚本:
scriptone.js scriptone.js
function FunctionOne(){
// Do a bit of work...
// Include a second javascript file, scripttwo.js
// that contains a function called FunctionTwo.js
var scrb = document.createElement('script');
scrb.type = 'text/javascript';
scrb.src = 'http://www.example.com/scripttwo.js?bunchofargs=varied';
// Append it to the head.
document.getElementsByTagName('head')[0].appendChild(scrb);
// Can't run the second function directly, because it may not be loaded quite yet,
// So use the Waiter function.
Interval = setInterval("Waiter()", 10);
// All done.
return;
}
function Waiter(){
if(window.FunctionTwo) {
clearInterval(Interval);
FunctionTwo();
}
}
scripttwo.js scripttwo.js
function FunctionTwo(){
document.write('something based on calling page');
}
This works fine with FF and IE, but not with Opera or Chrome. 这适用于FF和IE,但不适用于Opera或Chrome。 In Chrome/Opera, everything seems to work ok in script one.
在Chrome / Opera中,一切似乎在脚本1中都可以正常工作。 However, nothing that should be happening in scripttwo.js actually happens.
但是,在scripttwo.js中应该发生的任何事情都不会发生。 It's as if scripttwo.js didn't get included.
就好像scripttwo.js没有被包括在内一样。
Any ideas why this doesn't work with Opera or Chrome? 任何想法为什么这不适用于Opera或Chrome?
Perhaps I'm using something that isn't compatible, or are there security features I am not aware of? 也许我正在使用不兼容的东西,或者是否有我不知道的安全功能? All files are on the same domain.
所有文件都在同一个域中。
Note Great replies - thanks so much! 注意很棒的回复 - 非常感谢!
FuncionOne is just a typo here, in the actual code, I use better function names, but I changed them here for readability. FuncionOne在这里只是一个拼写错误,在实际代码中,我使用了更好的函数名称,但为了便于阅读,我在这里更改了它们。 It may be the scope, though i agree with Joe White that it shouldn't be an issue.
这可能是范围,但我同意乔怀特认为它不应该是一个问题。 With JavaScript (one of my weak languages), who knows?
使用JavaScript(我的弱语言之一),谁知道? FunctionOne is called from either the head or body of the HTML document.
从HTML文档的头部或主体调用FunctionOne。
I also like the idea of adding FuncTwo to the end of script two, to avoid the timer altogether. 我也喜欢将FuncTwo添加到脚本2的末尾,以完全避免使用计时器。 Cleaner, and so obvious once somebody points it out to you...
清洁,一旦有人向你指出,那么显而易见......
I will update after I work on this next. 我将在接下来的工作后更新。
Update Again: 再次更新:
Hi All, 大家好,
I now have it working in FF, IE, and Chrome, but Opera seems to refuse to load any .js files at all now. 我现在有它在FF,IE和Chrome中工作,但Opera现在似乎拒绝加载任何.js文件。 I'm thinking this is just an Opera issue of some sort ( Opera: .js file won't load ), and will proceed with the other three.
我认为这只是某种类型的Opera问题( Opera:.js文件不会加载 ),并将继续其他三个。 Let you know how it turns out.
让你知道结果如何。
It works for me in Opera.. 它在Opera中适用于我..
Instead of using the Waiter script, you could use an event: 您可以使用以下事件代替使用Waiter脚本:
scrb.onload = function() { FunctionTwo() }
scrb.onreadystatechange = function() { FunctionTwo() }
The second line is for Internet Explorer to work. 第二行是Internet Explorer工作。 A problem there is that Opera seems to hanle both these events, so FunctionTwo() would be executed twice.
一个问题是Opera似乎处理这两个事件,因此FunctionTwo()将被执行两次。 There are various ways to get around that.
有各种方法来解决这个问题。 Browser detection, some global variable, etc.
浏览器检测,一些全局变量等
You could probably just add FunctionTwo() to the end of scripttwo.js. 您可以将FunctionTwo()添加到scripttwo.js的末尾。 Then it will run when it loads without the additional complexity of the interval.
然后它将在加载时运行,而不会增加间隔的复杂性。
Some questions/comments that might give you an answer: 一些问题/评论可能会给你一个答案:
What calls FuncionOne
(notice your spelling)? 什么叫
FuncionOne
(注意你的拼写)?
Timers are messy and don't always recurse (fire again unless stopped). 定时器很乱,并不总是递归(除非停止,否则再次开火)。 I would refactor
Waiter()
to check Interval
still exists and if not, create a recursive window.setInterval
. 我会重构
Waiter()
来检查Interval
仍然存在,如果没有,则创建一个递归的window.setInterval
。
On that note, you might need to explicitly specify window.setInterval
, not omitting the window.
在这方面,您可能需要显式指定
window.setInterval
,而不是省略window.
Is the scope of Interval
working? Interval
工作范围是否有效? Your define it within a function. 您在函数中定义它。 Traditional logic would say that
clearInterval(Interval);
传统逻辑会说
clearInterval(Interval);
in Waiter()
would not have access... But JS is a little messy like that. 在
Waiter()
没有访问权......但JS有点混乱。 Opera and Chrome might be a little less messy than you're hoping for. Opera和Chrome可能比你希望的更乱。 Merely defining it outside any function scope should fix that.
仅在任何功能范围之外定义它应该解决这个问题。
I think the problem is with the Interval
scope. 我认为问题在于
Interval
范围。 It is defined inside FunctionOne, but not in a global scope. 它在FunctionOne中定义,但不在全局范围内定义。 So, I suspect that when it comes to execute
Waiter
, Opera and Chrome encounters that Interval
is undefined
and just silently drops out of FunctionTwo (maybe stops script?). 因此,我怀疑在执行
Waiter
,Opera和Chrome会遇到Interval
undefined
且只是默默地退出FunctionTwo(可能会停止脚本?)。 FF and IE may just ignore this. FF和IE可能会忽略这一点。
(BTW, what clearInterval
should canonically do when it receives undefined parameter value?). (顺便说一句,当接收到未定义的参数值时,
clearInterval
应该规范地做什么?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.