JavaScript - Single Or Double Quotes
Hi
Can you please advise on this. When writing javascript what is best practice.... single or double quotes? I tried to research this and everyone contracdicts each other? thanks A Similar TutorialsHi, I have completed the necessary function and it does not need any changes however from the "alert" I need there has to be double quotes surrounding the search 'Lboro'. (I know I may be making a meal of the coding however the lecturer wants us to follow this due to everyone being at different levels of programming). I have used the '\' character however the double quotes do not end up in the position I require them?! My Coding function findAnyU (s){ var a = s , b , c , d , e = -1; for (var i = 0 ; i < pages.length && e == -1 ; i++){ b = pages[i].indexOf('['); c = pages[i].indexOf(']'); d = pages[i].substring(b+1, c).toLowerCase(); e = d.indexOf(s.toLowerCase()); } if(e >= 1) a += ' found' else a += ' not found' return (a) } alert (findAnyU('Lboro' , pages)); Current Alert = Lboro found Cheers George I've got this bit of code that I've wrote and it was working absolutely fine like this: Code: function validatepostcode() { var postcodevalue = document.getElementById("postcode").value; if (postcodevalue.indexOf("S") == -1) { alert(postcodevalue + " is not a valid postcode."); } } Until I tried to add double quotes: Code: function validatepostcode() { var postcodevalue = document.getElementById("postcode").value; if (postcodevalue.indexOf("S") == -1) { alert( \" postcodevalue \" + " is not a valid postcode."); } } What I want to do is wrap the value of "postcodevalue" in double quotes, and since it's inside an alert, I used the escape sequence \", which is how it should be done as far as I know. But for some reason, Dreamweaver's giving me a syntax error warning and the code doesn't work. Am I using the double quotes wrong? Regards, Hashim. How can I add single quotes around listObj.options[i].value. I still want to keep the comma I'm adding to the end of it. Code: CSV += listObj.options[i].value + ','; Hi , I have a data coming as a string which contains single quotes and double quotes but it is showing up as Code: ' and " it should show it as Code: ' and " How do I fix this issue. in JavaScript. Ok know those people who are in the bad habit of double clicking everything? Well my site breaks if they double click it... is there a script I can use that won't let my functions run more then once every so many seconds? to avoid double clicking errors? action3.innerHTML = '<input type="button" class="menuButtons" id="menuButton1" value="Lab1" onclick="requestWindow("e;../lab1.html"e;)" />... how can I fix that. I tried "es for a third quote after already using single quotes and double quotes but it still hates me =[ I am just learning the basics and am trying to figure out timing with setTimeout() Code: <script type="text/javascript"> document.write("<p>" + Date() + "</p>"); setTimeout("document.write('Hello There World');", 2000); setTimeout("document.write("<p>" + Date() + "</p>");", 2000); </script> the 1st document.write works and the 1st setTimeout() works, but then I add the fourth line and the page will not load. Why does it not like my 4th line : "setTimeout("document.write("<p>" + Date() + "</p>");", 2000);"? This little snippet of code produces random quotes but sometimes gives a missing quote between two quote marks. Code: <HTML> <HEAD> <TITLE>Javascript - RANDOM QUOTES</TITLE> <script type="text/javascript"> function quote() { //Random Quotes } ; q = new quote() ; n = 0 // Quote database------------------------------------ q[n++]= "<FONT FACE =\"Trebuchet MS\" SIZE=\"2\" COLOR='#0006C7'>'QUOTE 1' (PERSON 1)</FONT>"; q[n++]= "<FONT FACE =\"Trebuchet MS\" SIZE=\"2\" COLOR='#0006C7'>'QUOTE 2' (PERSON 2)</FONT>"; </script> </HEAD> <BODY> <HR> <script type="text/javascript"> i=Math.floor(Math.random() * n) ; document.write( q[i] ); </SCRIPT> <HR> </BODY> </HTML> Can someone explain why? I use around 50 quotes in the real thing. Code: <html> <body> <div id="link"> <a href="http://www.facebook.com/">Facebook</a> </div> <script type="text/javascript"> var addr=new Array(); addr[0]="http://www.google.com/"; addr[1]="http"//www.yahoo.com/"; addr[2]="http://www.bing.com/"; var rl=document.getElementById("link"); var ri=Math.floor(Math.random()*3); rl.innerHTML= </script> </body> </html> I got stuck in getting this to work (show random element of the array as a link with description) Code: rl.innerHTML='<a href="addr[ri]">addr[ri]</a>'; This is showing addr[ri] on a page and directs to www.domain.com/addr[ri] Thank you in advance for any info. i've made an app in php/mysql which generates random quotes at intervals. i'm trying to integrate it with facebook. if i use iframes, everything works smoothly except for one issue, that facebook doesn't support iframe in profile boxes - which is what i want. for this reason, i prefer to use fbml. however, i'm using jquery to get the quotes from the php file, and this doesn't work in fbml. i read that with fbml, i can work with fbjs. since i do not know fbjs, i'm stuck. how do i convert the jquery code to get the quotes from php to fbjs? specifically, which part from the jquery file do i need to convert to fbjs, to get my code to work using fbjs? any help would be appreciated. i tried using fbjqry, but it didn't work. this is my code: Code: <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css/jquote.css" /> <!--<script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>--> <script type="text/javascript" src="fbjqry/utility.js"></script> <script type="text/javascript" src="fbjqry/fjqry.js"></script> <script type="text/javascript"> // On page load, fill the box with content. $(document).ready(function() { $("#quoteContainer").load("quote.php"); }); var auto_refresh = setInterval( function () { $('#quoteContainer').load('quote.php'); }, 5000); // refresh every 10000 milliseconds </script> </head> <div id="wrapper"> <div class="header"> Quote of the Day</div> <div id="quoteContainer"> </div> </div> </html> This is my very first javascript (I have limited knowledge C++ and php). I am trying to make an instant quote calculator, but having some trouble. I want to display the result of the calculation in the textbox with id price. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>OVP Custom Order</title> <script language="javascript"> <!-- function calcul(){ var vmin = document.getElementById('dur_min')[document.getElementById('dur_min').selectedIndex].value; var vsec = document.getElementById('dur_sec')[document.getElementById('dur_sec').selectedIndex].value; var vscript = document.getElementById('script')[document.getElementById('script').selectedIndex].value; var vvstock = document.getElementById('vis_stock')[document.getElementById('vis_stock').selectedIndex].value; var vvvideo = document.getElementById('vis_video')[document.getElementById('vis_video').selectedIndex].value; var vvanim = document.getElementById('vis_anim')[document.getElementById('vis_anim').selectedIndex].value; var vvoice = document.getElementById('voice')[document.getElementById('voice').selectedIndex].value; var vmusic = document.getElementById('music')[document.getElementById('music').selectedIndex].value; vmin = vmin*1; vsec = vsec*1; vscript = vscript*1; vvstock = vvstock*1; vvvideo = vvvideo*1; vvanim = vvanim*1; vvoice = vvoice*1; vvmusic = vvmusic*1; var overhead = 547; cost = (vmin+vsec)*(vscript+vvstock+vvvideo+vvanim+vvoice+vmusic+overhead); document.getElementById('price').value = cost; } // --> </script> </head> <body> <form id="form1" name="main"> <label>Duration <select id="dur_min"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> </select> </label> mins <label> <select name="dur_sec" id="dur_sec"> <option value="0">00</option> <option value="0.25">15</option> <option value="0.5">30</option> <option value="0.75">45</option> </select> </label> seconds<br /> <label><br /> Script Provided by <select name="script" id="script"> <option value="0">Client</option> <option value="200">OVP</option> </select> </label> <br /> <br /> Visuals<br /> <label> <input value="100" type="checkbox" name="vis_stock" id="vis_stock" /> Stock Image Library</label> <br /> <label> <input value="300" type="checkbox" name="vis_video" id="vis_video" /> Video Shoot</label> <br /> <label> <input value="200" type="checkbox" name="vis_animation" id="vis_animation" /> Animation & Graphics</label> <br /> <label><br /> Voiceovers <select name="voiceovers" id="voiceovers"> <option value="0">No</option> <option value="200">Yes</option> </select> </label> <br /> <label><br /> Royalty Free Music <select name="music" id="music"> <option value="0">No</option> <option value="50">Yes</option> </select> </label> <br /> <br /> Output Format(s)<br /> <label> <input type="checkbox" name="out_web" id="out_web" /> Web</label> <label> <input type="checkbox" name="out_dvd" id="out_dvd" /> DVD</label> <label> <input type="checkbox" name="out_download" id="out_download" /> Download</label> <br /> <br /> <label> <input type="submit" name="submit" id="submit" value="Calculate" onClick="calcul()"/> </label> <label>Approximate Price <input name="price" type="text" id="price" size="10" virtual warp="on" /> </label> <br /> </form> </body> </html> Thanks in advance Good morining, I'm working on a javascript for a rollover menu that the client is regularly changing. I want to create a call to a remote "document write" script on each page so that when it changes I only have to modify it in the script rather than on every page of the site. The problem I am having is that I cannot figure out how to include the quotes needed to execute the rollovers without killing the script. I tried removing all the quotes, and the "\" method as I would with a perl script, (and MANY other attempts) but no go. Here is where I'm at right now. I would appreciate any suggestions or links to info I can read to learn from. Call to the script in the html page(s): Code: <SCRIPT language="JavaScript" SRC="Nav_11Set.js"></SCRIPT> The script (condensed to one image): Code: document.write('<table id=Table_01 width=143 height=264 border=0 cellpadding=0 cellspacing=0><tr><td><a href=http://www.minersalley.com/Memberships.html onmouseover=changeImages('Nav_11Set_01', 'images/Nav_11Set_01-over.jpg'); return true;onmouseout=changeImages('Nav_11Set_01', 'images/Nav_11Set_01.jpg'); return true;onmousedown=changeImages('Nav_11Set_01', 'images/Nav_11Set_01-down.jpg'); return true; onmouseup=changeImages('Nav_11Set_01', 'images/Nav_11Set_01-over.jpg'); return true;><img name=Nav_11Set_01 src=images/Nav_11Set_01.jpg width=143 height=24 border=0 alt=Membership Info!></a></td></tr></table>') Hey all, have a bit of a problem. I'm trying to store a variable in Javascript that consist of user input that may include both quotes and apostraphes (amongst any other stuff). Currently looks a bit like this: Code: <script> var importantNote = [[Note]]; if (importantNote != "" && importantNote != " ") { var importantNoteOutput = "<div class=\"important\"><h3>Important Note:</h3> [[Note]]</div>"; document.write (importantNoteOutput); } </script> Obviously, when [[Note]] (another variable pulled through a seperate program to create complete code) contains ", it doesn't go so well ('[[Note]]' solves that, but creates same issue with any plurals ). I *could* get end user (probably me) to always ensure anything entered in this field (and few others used in a similar way) contain escaped characters, but this isn't preferable. Preferably, I'd like a way of escaping an entire string automatically, eg. encode([[Note]]) - though that doesn't seem to work. Any quick tips? FWIW, I don't think I can use php, or much else for the task. Pretty much stuck with html and javascript. Thanks I'm trying to insert a title and a body into a Google maps window through PHP. The body could contain double or single quotes. How do I output these so that they don't break the script? Relevant code: Code: $('#map_canvas').gmap('openInfoWindow', { 'content': '<strong><?php echo $m->marker->Title; ?></strong><br /><?php echo $m->marker->Body; ?><?php echo '<a href="./marker_info.php?id='.$m->marker->Nid.'"></a>' ?><br /><a id="m_location" href="#" data-role="button" data-icon="search" onclick="$.mobile.silentScroll(500);"></a>'}, this); the following javascript code is for a page that has to different login sections (username and password) hospital admin i want them to login in their part using normal IDs(numbers) and password, and normal users to login in the other login boxes (using email address and password) , each login boxes have their own submit button. the problem is i can't check each of them alone. what i want to do is that if (for user) email and password entered and pressed his login button, to be headed to another page, while if hospital admin entered his id and password in his section and pressed login button, to be headed for a different page. help please ! Code: window.onload = initForms; function initForms() { for (var i=0; i< document.forms.length; i++) { document.forms[i].onsubmit = function() {return checkFields();} } } function checkFields(){ if(document.getElementById("userLogin").onclick) { if(isEmail(document.getElementById("userUsername").value)==false) { alert("Please enter a valid e_mail address."); document.getElementById("userUsername").focus(); return false; } if(document.getElementById("userUsername").value=="") { alert("Please enter your e_mail address."); document.getElementById("userUsername").focus(); return false; } if(document.getElementById("userPassword").value=="") { alert("Please enter your password."); document.getElementById("userPassword").focus(); return false; } } if(document.getElementById("hospLogin").onclick) { if(document.getElementById("hospUsername").value=="") { alert("Please enter your ID."); document.getElementById("hospUsername").focus(); return false; } if(document.getElementById("hospPassword").value=="") { alert("Please enter your password."); document.getElementById("hospPassword").focus(); return false; } } } function isEmail(mail) { var char1 = mail.split("@"); var y = mail.indexOf("@", 0); var e = mail.indexOf(".", 0); var w = mail.lastIndexOf(".", mail.length - 1); var sp = mail.indexOf(" ", 0); var pl = mail.indexOf("@.", 0); for (i = 0; i < mail.length; i++) { if (mail.charAt(i) == "/" || mail.charAt(i) == ":" || mail.charAt(i) == ";" || sp > 0 || pl > 0 || mail.charAt(i) == '"' || char1.length > 2 || y == -1 || y == 0 || e == 0 || w == mail.length - 1 || e == -1) { return false; } } } and there goes the HTML codes: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="script.css" /> <script type="text/javascript" src="script.js"></script> <title>Blood high res END</title> </head> <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <div id="apDiv4" style="background:url(images/Blood-high-res-END_04.jpg) no-repeat"></div> <div id="apDiv1" style="background:url(images/Blood-high-res-END_01.jpg)"> <p>Hospital</p> <form name="hospForm" id="hospForm"> <input type="text" name="hospUsername" id="hospUsername" class="reqd" /><br /> <input name="hospPassword" type="password" id="hospPassword" class="reqd" /><br /><br /> <input name="submit" type="submit" id="hospLogin" value="Hospital log in" /></form></div> <div id="apDiv2" style="background:url(images/Blood-high-res-END_02.jpg)"></div> <div id="apDiv3" style="background:url(images/right.jpg)"> <a id="register" href="../User Registration Page/page.html" target="_self">Register Now</a> <a id="links" href="../Links/page.html" target="_self">Links</a> <p>Visitor</p> <form name="userForm" id="userForm"> <input type="text" name="userUsername" id="userUsername" class="reqd" /><br /> <input name="userPassword" type="password" id="userPassword" class="reqd" /><br /><br /> <input name="submit" type="submit" id="userLogin" value="User log in" /><br /></form> <a id="register2" href="../User Registration Page/page.html" target="_self">Register now</a><br /> <a id="forgot" href="../User Forgot Password/page.html" target="_self">Forgot Password</a></div> <div id="apDiv5" style="background:url(images/Blood-high-res-END_05.jpg) no-repeat"></div> <div id="apDiv6" style="background:url(images/Blood-high-res-END_06.jpg) no-repeat"> <p>Hello and welcome to Red Drop. By visiting our page,<br />you ensure to each and every one of the team crew <br />behind the stage, and to every lebanese person that<br />you are mentally prepared to donate a few drops of your<br />blood to the benefit of an anonymous stranger.<br /><br />With every drop you donate, you might be giving<br />a father, a mother or a child another day to live among<br />his family and his beloved persons.<br /><br />Register today and add donation blood to your frequently<br />social and humanitarian activities.</p> </div> <div id="apDiv7" style="background:url(images/Blood-high-res-END_07.jpg) no-repeat"></div> <div id="apDiv8" style="background:url(images/Blood-high-res-END_08.png) no-repeat"></div> <div id="apDiv9" style="background:url(images/Blood-high-res-END_09.jpg) no-repeat"></div> </body> </html> I havent added the server side scripts yet, i am planning to validate forms as much as possible using javascript first, and this is not for a real hospital, it's a graduation project, so to explain the point, on the page i have 2 seperated forms wich each having its own submit button, one to be used by hospital admins, and other to be used by users. For admins i just want to make sure their fields are not empty. For users i want to make sure their fields are not empty and their email field is valid. but when trying it, it's kinda only checks for the user fields. P.S: I apologize about not using codes, i didn't knew about this option. here is where the code is. http://simplythebest.net/scripts/DHT...script_15.html I need this to be able to open in the parent window not in another window. Or if someone could precribe a double drop box that works in "moonfruit" that would be great. Thank you very much for your time. David I've been working with this script Code: function changeImage(layer_id, item_id, position) { var theDiv = document.getElementById(layer_id); var theImg = (theDiv != null) ? theDiv.firstChild : null; if (theImg != null) { // If the item id is 0, then just make the image empty lol if (item_id == 0) { theDiv.parentNode.removeChild(theDiv) } else { theImg.setAttribute('src', 'images/' + layer_id + '-' + item_id + '.png'); } } else { var toAppend = '<div id="' + layer_id + '" style="position: absolute; z-index: ' + position + ';"><img src="images/' + layer_id + '-' + item_id + '.png" /></div>'; document.getElementById("item-images").innerHTML += toAppend; } } where layer_id, item_id, and position are all defined elsewhere. I've been trying to get the images to append, and then run this line Code: theDiv.parentNode.removeChild(theDiv) when you click on them consecutively(click to append, click again to remove, so three consecutive clicks would have the image shown and four would not), but all I've succeeded at so far is breaking the script's functionality. Help? hello I have a listbox full of names on a PHP website and was wondering if there was a way to grab the value during a double click to bring me to another form to display the name and information? |