Jump to content

Troubles with quotes


Mikeyham

Recommended Posts

I'm new to javascript, and want to write a switch statement that will make a button appear which will call a function with an argument that will display what day it is. The problem is, the way I'm doing it, there are too many quotation marks within other quotation marks. For example: var d = new Date();theDay=d.getDay();function displayDate(txt){alert(txt)}switch (theDay){case 0:document.write("<input type='button' value='Display Day' onclick='displayDate('Today is Sunday')' <== RIGHT HERE ...Today is Sunday is contained between the quotation marks within the argument parentheses thingy, which is contained within the attribute quotation marks, which is contained within the document.write quotation marks.I know that if you need to put quotation marks within other quotation marks, you use ' , but is there a 3rd "tier"? Thanks!

Link to comment
Share on other sites

You will need to escape the ' (place a \ before the second ' mark).document.write('O\'Reilly');JavaScript will think you ended the string, and give an error when it doesn't recognize the rest, and when you join strings together, you would use + operator, and that would be another error since you didn't escape the string.

Link to comment
Share on other sites

mma_fighter123 speaks the truth. But there are larger truths at stake here. What follows is my best advice.1. document.write() is an okay learning instrument, and I know that's where you're at. But don't go thinking it's normal javascript. It's not. Good scripts almost never use it. If you want to test your scripts in development, stick to alert messages.2. Good pages do not define event handlers in the HTML markup. I know you've seen it done this way, but the technique is a holdover, and current best practices dictate the technique I'm about to show you. Among other things, this makes it easier to handle quotation marks. :)This little script has some unfamiliar ideas, maybe. And at first it'll seem like a lot of extra work. For a little test page, maybe it is. But when your pages and scripts start to grow, you'll appreciate having this foundation. Eventually, you'll define all your event handlers this way, and they'll all be in the same place, so they'll be easy to find and easy to edit. The page markup will be easier to read as well.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">		<title></title>		<script type="text/javascript">			function init () {				document.getElementById("test_button").onclick = function () {					alert (this.id)				};			}			window.onload = init;		</script>	</head>	<body>		<div>			<button type="button" id="test_button">Click me</button>		</div>	</body></html>

Link to comment
Share on other sites

Hi Guys, I have my own sample, i wrote something like mikeyham would like to write but I am not sure if this is right for what i assumed he would like to happend. Kindly review thanks.

<html><head><script type="text/javascript">function dayToday () {var t = new Date();var day = t.getDay()+1;switch (day) {case 1: alert("Today is Sunday");break;case 2: alert("Today is Monday");break;case 3: alert("Today is Tuesday");break;case 4: alert("Today is Wednesday");break;case 5: alert("Today is Thursday");break;case 6: alert("Today is Friday");break;case 7: alert("Today is Saturday");break;}}</script></head><body><button onclick="dayToday()">Today is</button></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...