javascript window.location.href 的 window.addEvent('domready',function() 的替代方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9133691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are alternatives to window.addEvent('domready',function() for window.location.href?
提问by Shane
We are passing the url page to our Admin Emails through the 'refer' id in Joomla using ...
我们正在使用 Joomla 中的“引用”ID 将 url 页面传递到我们的管理员电子邮件...
window.addEvent('domready', function()
{
document.getElementById('refer').value=window.location.href;
});
This code works on some sites but not others. I have tried a jquery version with little luck, though I am open for suggestions. A site not returning the url via email is at http://www.freestylelitemeter.comwhile a site that is working is at http://www.comparediabetictestingsupplies.com. We are using 'refer' as a hidden field and everything matches, so I believe the issues is with the window.addEvent('domready', function() unless there is a conflict I am not aware of. Another interesting thing is the working domain has many more script files that has in the past found conflict between script files, while the smaller site has not.
此代码适用于某些网站,但不适用于其他网站。我尝试了一个 jquery 版本,但运气不佳,尽管我愿意接受建议。未通过电子邮件返回 url的站点位于http://www.freestylelitemeter.com ,而正在运行的站点位于http://www.comparediabetictestingsupplies.com。我们使用“引用”作为隐藏字段并且所有内容都匹配,所以我相信问题出在 window.addEvent('domready', function() 除非有我不知道的冲突。另一个有趣的事情是工作域有更多的脚本文件,过去发现脚本文件之间存在冲突,而较小的站点则没有。
回答by gilly3
At http://www.freestylelitemeter.com/, MooTools fails to load. You get this error in your JavaScript console:
在http://www.freestylelitemeter.com/ 上,MooTools 无法加载。您在 JavaScript 控制台中收到此错误:
SCRIPT438: Object doesn't support property or method 'getElement'
mootools.js, line 53 character 97
SCRIPT438:对象不支持属性或方法“getElement”
mootools.js,第 53 行字符 97
MooTools extends window
with addEvent()
. Since MooTools hasn't loaded, addEvent()
fails and your code is never executed.
MooTools 扩展window
为addEvent()
. 由于 MooTools 尚未加载,因此addEvent()
失败并且您的代码永远不会执行。
You have a buggy version of MooTools, correct that and your problem should be resolved.
您有一个有问题的 MooTools 版本,纠正它,您的问题应该得到解决。
But, better yet, you are already using jQuery. Just change your code to this:
但是,更好的是,您已经在使用 jQuery。只需将您的代码更改为:
$(function ()
{
$('#refer').val(location.href);
});
Are you even using MooTools much? You may be able to switch to jQuery exclusively, and load one fewer library.
你甚至经常使用 MooTools 吗?您也许可以专门切换到 jQuery,并少加载一个库。