View Full Version : including an 'else' after 'document.write'
Jedi Knight
02-26-2008, 05:17 AM
<script type="text/javascript">
var str="Free Web Tutorials!";
document.write(str.link("http://www.w3schools.com"));
</script>
I'm utilizing the above string on a page.
I would like to include a statement for browsers with javascript disabled, to replace the link with 'Please enable Javascript to continue'.
Thanks for any suggestions.
I remember how to do it, sorry.
<noscript>I'm an Idiot</noscript>
_Aerospace_Eng_
02-26-2008, 05:36 AM
You would use this
<script type="text/javascript">
var str="Free Web Tutorials!";
document.write(str.link("http://www.w3schools.com"));
</script>
<noscript><p>Please enable Javascript to continue</p></noscript>
however you shouldn't be using document.write. Instead you should use DOM functions. If you more than one link then you would do something like this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
var links = new Array();
links[0] = 'http://www.w3schools.com,Free Web Tutorials!';
links[1] = 'http://www.codingforums.com,Coding Forum';
links[2] = 'http://www.google.com,Search Engine';
links[3] = 'http://www.yahoo.com,Games';
links[4] = 'http://www.msn.com,News';
function writeLinks()
{
for(var i = 0; i < links.length; i++)
{
document.getElementById('link'+i).setAttribute('href',links[i].split(',')[0]);
document.getElementById('link'+i).appendChild(document.createTextNode(links[i].split(',')[1]));
}
}
window.onload = writeLinks;
</script>
</head>
<body>
<a href="" id="link0"></a>
<a href="" id="link1"></a>
<a href="" id="link2"></a>
<a href="" id="link3"></a>
<a href="" id="link4"></a>
<noscript><p>Please enable Javascript to continue</p></noscript>
</body>
</html>
If you only have one link then something like this is fine.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
function writeLink(where,url,val)
{
document.getElementById(where).setAttribute('href',url);
document.getElementById(where).appendChild(document.createTextNode(val));
}
window.onload = function()
{
writeLink('link0','http://www.w3schools.com','Free Web Tutorials!');
}
</script>
</head>
<body>
<a href="" id="link0"></a>
<noscript><p>Please enable Javascript to continue</p></noscript>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.