Internet Explorer 7以降でページを読み込むたびに、Object Expected
以下の関数を呼び出すとエラーが発生します。</body>
スクリプトは、ページの下部の終了タグの直前に表示されます。name
同じまたは。を持つ要素はありませんid
。
<script type="text/javascript">
window.onload = show();
</script>
呼び出そうとしているJavascript関数は
function show() {
obj1
= document.getElementById("container").innerHTML
= '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
+ '<p><a href="#" onclick="hide();">test</a></p></div>';
}
- このエラーが他のブラウザに表示されないのはなぜですか?
- 問題を解決するには何を変更する必要がありますか?
編集0
関数show
をで同じブロックwindow.onload
に移動すると、機能しhide()
なくなります。
Javascriptコード
function show() {
obj1
= document.getElementById("container").innerHTML
= '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
+ '<p><a href="#" onclick="hide();">test</a></p></div>';
}
function hide() {
obj1 = document.getElementById("container");
if(obj1){
alert("Hi");
obj1.style.display = "none";
obj1.style.visibility = "hidden";
}else{
alert("Cannot find the element with id container.");
}
}
HTMLコード
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>takeover</title>
<base href="" />
<link rel="stylesheet" href="" />
<style type="text/css" media="all" />
#container {
position:absolute;
text-align:center;
background-color:#fff;
z-index:9999;
}
</style>
<script type="text/javascript" src="takeover.js"></script>
</head>
<body>
<div>
<div id="container"></div>
<p><a href="#">qwe</a></p>
</div>
<script type="text/javascript">
window.onload = show;
</script>
</body>
</html>
編集1
alert(show)
以前に配置したときにInternetExplorer以外のブラウザを使用したときに表示されるメッセージwindow.onload
編集2
すべての空白を削除した後に表示されるメッセージ。繰り返しますが、これはInternetExplorer以外のブラウザでのみ機能します。
編集3
window.show = function show()およびwindow.hide = function hidden()を試しましたが、InternetExplorerでエラーが発生します。エラーは以下のように表示されます。
編集4
これは、すべての関数が1つのファイルに含まれている更新されたコードです。これは他のブラウザでは機能せず、エラーshow
が未定義であることがわかります。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>takeover</title>
<base href="" />
<link rel="stylesheet" href="" />
<style type="text/css" media="all" />
#container {
position:absolute;
text-align:center;
background-color:#fff;
z-index:9999;
}
</style>
</head>
<body>
<div>
<div id="container"></div>
<p><a href="#">qwe</a></p>
</div>
<script type="text/javascript">
alert(show)
window.show = function show() {
obj1 = document.getElementById("container").innerHTML = '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p><p><a href="#" onclick="hide();">test</a></p></div>';
}
window.hide = function hide() {
obj1 = document.getElementById("container");
if(obj1)
{
alert("Hi");
obj1.style.display = "none";
obj1.style.visibility = "hidden";
}
else
{
alert("Cannot find the element with id container.");
}
}
window.onload = window.show;
</script>
</body>
</html>