JavaScript - Escaping Double Quotes In Alert
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. 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 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 I am getting double Alert prompts for a single form: The alert pops up the first time, then when I click OK, the same one pops open again. I think my code should do the alert once when it hits "else window.alert(sameWarning) and the stop when it reaches "return false()"; but it seems return false() is causing the alert twice. Maybe because I have two forms on one page; yet, they have two separate function names ("signinForm()" and "saveSignUp()")and call two separate forms (forms[0] and forms[1])? I think my code is correct, but I can't figure it out at this point. Any help would be appreciated. Thanks in advance! Code: <script type="text/javascript"> //FIRST FUNCTION FOR FORMS[0] function signinForm(){ // var sameWarning = "The required fields were not submitted for the following error(s). \n \n"; var warningLogin = "The following field(s) require an entry: \n \n"; var sameWarning = warningLogin; var userName = document.forms[0].userName.value; var pswd = document.forms[0].pswd.value; if(userName == ""){ sameWarning += " - First Name \n"; } if(pswd == ""){ sameWarning += " - Password \n"; } if (warningLogin == sameWarning){ return true; } else { window.alert(sameWarning); } return false; } //SECOND FUNCTION CALL FOR FORMS[1] function saveSignUp(){ // var warning = "The required fields were not submitted for the following error(s). \n \n"; var warning = "The following field(s) require an entry: \n \n"; var same = warning; var firstName = document.forms[1].firstName.value; var lastName = document.forms[1].lastName.value; var email = document.forms[1].email.value; var phone = document.forms[1].phoneNumber.value; if(firstName == ""){ //firstName.style.backgroundColor="red"; warning += " - First Name \n"; } if(lastName == ""){ warning += " - Last Name \n"; } if(email == ""){ warning += " - Email \n"; } if(phone == ""){ warning += " - Work Phone \n"; } if(phone < 5 ){ warning += " - Must be a numberssss \n"; } if (warning == same){ return true; } else { alert(warning); } return false; } </script> HTML Code: <h1>Login</h1> <form method="post" onsubmit="return signinForm();" action="" > <input type="text" placeholder="Username/Email" name="userName"> <input type="password" placeholder="Password" name="pswd" id="pswd"> <input type="submit" onclick="signinForm();" value="Sign In"> </form> <h1>Sign Up!</h1> <form method="post" onsubmit="return saveSignUp()" action="" > <input type="text" placeholder="First Name" name="firstName"> <input type="text" placeholder="Last Name" name="lastName"> <input type="text" placeholder="Email" name="email"> <input type="text" placeholder="Phone Number" name="phoneNumber"> <input type="submit" onclick="saveSignUp();" class="button wide" value="Request an Account"> </form> *UPDATE Fixed! After I removed the onclick in the button, the double alerts went away. Apparently, I was I was firing off two events (calling the javascript twice) with both the onsubmit and the onclick. Just wondering: 1. Is "onsubmit" a special event handler just for button "type=submit" vs onlclick can be used for any element? 2. Is there a preference among developers to use onclick vs. onsubmit? 3. Is there an order of precedence for method calls over submit calls (the onclick goes first, then it automatically fires the submit call to the function too)? I have an user table like this:- guid | username | password | firstname | lastname | location | emailad dress | userrole -----------------------------------+----------+----------------------------------+-----------+-----------+----------+-------- ------+--------------- 8024259764dc3e8ee0fb6f5.84107784 | james | 827ccb0eea8a706c4c34a16891f84e7b | james | bond | NY | ny@live .com | administrator 18689183644dc3e91571a364.71859328 | saty | 250cf8b51c773f3f8dc8b4be867a9a02 | saty | john | NY | hk@fd.c om | administrator 2644885344cecd6f2973b35.63257615 | admin | 21232f297a57a5a743894a0e4a801fc3 | System | Generated | | | administrator (3 rows) now my postgre query for delete the row .... $query = "delete from users where username!= 'admin' and guid='".$guid."'"; $result = pg_query($conn, $query); ?> <script type="text/javascript"> alert("Cannot delete this .\n It is system generated(s)."); </script> <?php (1)when I delete the user name one by one then delete occurs in my page userlist.php, I donot want to delete admin so i use username!= 'admin' in where condition as shown above. (2)now when I del any username(3 rows) from user table then alert occurs & it delete from userlist.php after that my page userlist.php is blank. Finaly when i refresh the page then my admin username seen.. when i use return true; function then only alert generate .. delete doesnot occurs ... Actauly i want:- (1)if user is not admin then it delete from userlist.php ... nd also i m continue on this page ... like when james and saty want to delte their acount ..as given in table. (2)if user is admin then alert generate nd i m continue on this page. i m tired now plz help me .... so can anyone put the best condition in my coding. All, If I want to do the following: Code: result="<h2><strong>Home</strong></h2>The site idea is pretty simple. There are creeps everywhere and we want to point them out. Killing time during work or school never hurt anyone either. Help us out and give your friends something to enjoy. Make sure to submit your <a href="testurl.html">Test</a>"; How can I escape the URL because after that I want to do: Code: document.getElementById('showdata').innerHTML = result; Hello All, Just a general question about the normal workflow in a professional development environment... This may not have a concrete answer, but just curious if there is an established protocol... Does a front end programmer usually receive strings escaped or does he/she have to process the strings themselves before sending them to functions? Thanks for any thoughts... Happy Holidays. Hi I want to pass a strings from JS client to PHP server and vice versa. Problem is that I can't find a simple method of escaping them that gives consistent results. I've tried escape, unescape, urlencode and urldecode, but there are problems with some characters eg + From what I've read, JSON is the most reliable way to do this. I've figured out how to use json_encode to get a php string to a javascript variable, but I can't go the other way. How do I get the JSON string that represents a javascript object? Thanks for any help. I wasn't quite sure where to post this request as its JSON, javascript and php, so please tell me if I'm in the wrong place. 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 =[ 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 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> 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);"? 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 + ','; 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. 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. 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>') 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. 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); 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 |