window onload与body onload javascript冲突

时间:2013-09-27 14:15:02

标签: javascript

我的客户将我们的JS代码放在<head>的网站中,并抱怨我们的代码没有被解雇。以下是客户的示例代码:

    <html>
    <head>
    <script type="text/javascript">
    function create() {
    try {
    var baseURL = "https://serv2ssl.vizury.com/analyze/analyze.php?account_id=VIZVRM1125";
    var analyze = document.createElement("iframe");
    analyze.src = baseURL;
    var node = document.getElementsByTagName("script")[0];
    node.parentNode.insertBefore(analyze, node);
} catch (i) {
}
}
 var existing = window.onload;
window.onload = function() {
if (existing) {
    existing();
}
create();
}
  </script>
 </head>

 <body onload="javascript:clientfunction();">
 <script type="text/javascript">

function clientfunction()
{
   //client code is replcad here
   document.write("Hello testing");
}
 </script>

  </body>
 </html>

在页面上加载clientfunction()正在调用,我们的create()函数未触发。

请有人帮我解释为什么我们的标签没有解雇,这个问题的替代解决方案是什么?

1 个答案:

答案 0 :(得分:-1)

复制粘贴并检查以下运行

<html>
<head>
<script type="text/javascript">
function create() {
alert("Gdfg");
try {
var baseURL = "https://serv2ssl.vizury.com/analyze/analyze.php?account_id=VIZVRM1125";
var analyze = document.createElement("iframe");
analyze.src = baseURL;
var node = document.getElementsByTagName("script")[0];
node.parentNode.insertBefore(analyze, node);
} catch (i) {
}
}

window.onload=create;
window.onload=clientfunction;
</script>
</head>

<body onload="clientfunction(); create()">
<script type="text/javascript">

function clientfunction()
{
alert("fdsfsdf");
//client code is replcad here
document.write("Hello testing");
}
</script>

</body>
</html>