I have the following line of code in my Javascript file which I would like to call the function hide
when the user clicks on the link test however it does not appear as a link on the page. What do
document.write('<a href="#" onclick="hide();>test</a>"');
EDIT 0
Based on the suggestions, the only reason I am using document.write is because I am attempting to display HTML over another page i.e. a takeover page. Would appreciate a better way of doing this. The intention is until the user clicks on "test" the preceding HTML would be displayed. When they click on test, the preceding HTML is hidden and the page content as it would normally display is shown.
function show() {
obj1 = document.getElementById("container");
obj1.style.position = "absolute";
obj1.style.top = "0px";
obj1.style.left = "0px";
obj1.style.width = "100%";
obj1.style.textAlign = "center";
obj1.style.zIndex = "9999";
obj1.style.visibility = "visible";
obj1.style.display = "inline";
obj1.style.backgroundColor = "#FFF";
document.write('<h1>Hello World!</h1><p>Have a nice day!</p>');
document.write('<a href="#" onclick="hide();">test</a>');
}
function hide() {
obj1 = document.getElementById("container");
obj1.style.display = "none";
obj1.style.visibility = "hidden";
}
Full Code
<!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:10;
}
</style>
<script type="text/javascript" src="takeover.js"></script>
</head>
<body>
<div>
<div id="container">abc</div>
<p><a href="#">test</a></p>
</div>
<script type="text/javascript">
window.onload = show;
</script>
</body>
</html>
EDIT 1
Okay, having scoured a multitude of forums it appears that document.write replaces the entire screen and hence the inability to call the appropriate div element. I have instead replaced the javascript above with the below however am unsure where it is the proper way of doing it.
function show() {
obj1 = document.getElementById("container").innerHTML ='<p>Hello World.<br>Here I am.</p>';
obj1 = document.getElementById("container").innerHTML +='<a href="#" onclick="hide();">test</a>';
}
function hide() {
obj1 = document.getElementById("container");
if(obj1)
{
alert("Going to hide the element");
obj1.style.display = "none";
obj1.style.visibility = "hidden";
}
else
{
alert("Cannot find the element with id container.");
}
}
,
and proper ids and then use jquery.hide and show as needed
– Chetter Hummin Jun 12 '12 at 03:16