JavaScript - Js Search Array For Substring Then Edit Entry
Hi Guys,
I am trying to make a function that will search for a substring, in this case 'http://instagr.am/p/' within a text area on a form then add '[embed]' & '[/embed]' tags around it. I have currently got the following code Code: <script type='text/javascript'> function show_alert() { str = formname.elements['inputid'].value; arr = (str.split(' ') + '<br />'); jQuery.each(arr, function() { if (arr.indexOf('http://instagr.am/p/') >= 0) { alert('An http://instagr.am/p/ link has been found'); //alert('It is entry ... in the array'); //Then edit the entry } } ); } </script> This breaks the contents of the textarea (at spaces) into an array then finds any entries with 'http://instagr.am/p/' in them (could be 'http://instagr.am/p/29fdghHdv'). Once it has found any and all of these entries it will add an '[embed]' code to the beginning and an '[/embed]' tag at the end. Example... http://instagr.am/p/vuHdeyfa2 is converted to: '[embed]http://instagr.am/p/vuHdeyfa2[/embed]' Then the changes must reflected in the textarea input "formname.elements["textareaid"].value = editedstring; Thankyou for your assistance. Similar TutorialsI created an array, whose entries looks like this: [41, "The bird flew into it's cage"] [33, "He drew fire from Joe"] [33, "Roger asked her her name"] [2, "I am awfully happy"] . I want to pull the sentence item out of some array entries. E.g., in the second entry, namely, arrayName[1], what I thought was the second item (the sentence), I could manage by invoking arrayName[1][1] . But to my dismay, this doesn't do the trick. arrayName[1][1] actually delivers up the second CHARACTER of the entry (the number "3"). I thought the comma between the number and the sentence would separate the items, although I "pushed" each entry into the array as one single entry. How do I "grab" the whole sentence? How do I push both items separately to achieve "one entry"? I'm having major pains trying to figure this out. I'm kind of new to Javascript, I need to open a text file from an external server, store each line in an array, then search that array for a certain word (HIGH), and if it exists then write something to the webpage, and if not, write something else. Here is what I have so far: Code: <html> <head> <title>Test</title> <script> <!-- function test(x) { if (wxd1txt.readyState === 4 && wxd1txt.status === 200) { // Makes sure the document is ready to parse and Makes sure it's found the file. var wxd1text = wxd1txt.responseText; var wxd1array = wxd1txt.responseText.split("\n"); // Will separate each line into an array var wxd1high = wxd1array.toString(); //Converting the String content to String //var highsearchreg = new RegExp("HIGH"); //var wxd1high = wxd1array[x].search(highsearchreg); document.write(wxd1high); if (wxd1high.search("HIGH") >= 0){ document.write("HIGH RISK");} else { document.write("NO RISK");} } } //--> </script> </head> <body> Hi! <script> <!-- var Today = new Date(); var ThisDay = Today.getDate(); var ThisMonth = Today.getMonth()+1; var ThisYear = Today.getYear(); var Hour = Today.getHours(); var Day2 = Today.getDate()+1; var Day3 = Today.getDate()+2; if (navigator.appName != "Microsoft Internet Explorer") { ThisYear = ThisYear + 1900;} if (ThisMonth < 10) { ThisMonth = "0" + ThisMonth;} if (ThisDay < 10) { ThisDay = "0" + ThisDay;} if (Hour == 2 || Hour == 22 || Hour == 23 || Hour == 0 || Hour == 1) { var wxHourd1 = 0600} else if (Hour >= 3 && Hour <= 10) { var wxHourd1 = 1300;} else if (Hour >= 11 && Hour <= 13) { var wxHourd1 = 1630;} else if (Hour >= 14 && Hour <= 16) { var wxHourd1 = 2000;} else if (Hour >= 17 && Hour <= 21) { var wxHourd1 = 0100;} //var wxurld1 = "http://www.spc.noaa.gov/products/outlook/archive/"+ThisYear+"/KWNSPTSDY1_"+ThisYear+""+ThisMonth+""+ThisDay+""+wxHourd1+".txt"; var wxurld1 = "http://www.spc.noaa.gov/products/outlook/archive/2010/KWNSPTSDY1_201005101300.txt" //(High risk day for testing) //document.write(wxurld1); //Use this to verify this section is working if (window.XMLHttpRequest) { wxd1txt=new XMLHttpRequest(); } else // IE 5/6 { wxd1txt=new ActiveXObject("Microsoft.XMLHTTP"); } wxd1txt.open("GET", wxurld1, true); wxd1txt.onreadystatechange = test(); // --> </script> </body> </html> When added to a webpage, nothing shows up except the "Hi!" and there are no errors in the Javascript Console in Google Chrome. Is this possible with Javascript, and if so, what am I doing wrong or not doing? Also, I have 2 URLs, one is a text file that has the HIGH text I want for an example, the other is the current file, which shouldn't have HIGH in it (unless the weather in the US turns really bad) Hey Guys, I'm looking to code something in JavaScript within an HTML page that asks the user for two numbers. When the user clicks Calculate, it subtracts them and multiplies the difference by 3. I've gotten that to work fine. However, I run into some trouble because the user may enter 31000, and the formula only works with 31. (So, if the user enters 31000 and 11000, the code has to cut those into 31 and 11 to plug into the formula.) I know I'll have to use (var).substring(0,1) but when I do this the program doesn't seem to run. I'm not sure if this isn't working because it's within <HEAD></HEAD> or because it's looking for letters and I only have integers in the variable. Here's the code I'm currently working with: Any help on how to extract the first two digits from "currentaltitude" and "finalaltitude" is appreciated. Thanks in advance!! Code: <html> <head> <script> function temp(form) { var currentaltitude = parseFloat(form.curAltitude.value, 10); var finalaltitude = parseFloat(form.finAltitude.value, 10); var calcaltitude = parseFloat(form.calcAltitude.value, 10); calcaltitude = (currentaltitude - finalaltitude) * 3; form.calcAltitude.value = calcaltitude; } </script> </head> <body> <FORM> Enter current altitude: <INPUT TYPE=text NAME="curAltitude" VALUE="" MAXLENGTH="15" SIZE=10> <p> Enter final altitude: <INPUT NAME="finAltitude" VALUE="0" MAXLENGTH="15" SIZE=10> <p> Click this button to calculate descent altitude: <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON onClick=temp(this.form)> <p> Begin descending <INPUT NAME="calcAltitude" READONLY SIZE=4> DME from the assigned crossing point. </FORM> </body> </html> Basically what i'm trying to do is check the data in an XML node to see if it contains a certain string. I'm completely new to javascript and this seems like something very simple, but I must not be using the correct terms in my searches. For instance here i'm checking to see if the data in an xml node is equal to "URGENT" if so I'm changing the css. Code: function DoesXmlContainThisNode(strXML, strNodeXPath) { var bolFlag // Load XML var myDOMDocument = new ActiveXObject('Msxml2.DOMDocument.3.0'); myDOMDocument.async = false; myDOMDocument.loadXML(strXML); // Parse out value var myNode = myDOMDocument.selectSingleNode(strNodeXPath) if( myNode != null ) { bolFlag = true } else { bolFlag = false } myNode = null return bolFlag } , Code: if (DoesXmlContainThisNode(strResponseXml, "Response/XMLResponse/*/Message/Body[text()='URGENT']")) { strStyle = 'URGENT' } And maybe I cant do it the same way, but how would I search for any instance of the word "urgent". Say my message has 4 sentences and if the wor durgent appears anywhere in it.. I want to change the stylesheet. I would think there is some way to use a wildcard or "contains" string... I see the text() class being used everywhere but only on exact matches. Any help is greatly appreciated. Thanks Hi all, I am having trouble trying to use the substring String method. Relevant HTML code: Code: <img id="news" src="../images/news.png" alt="News" onmouseover="change(this)" /> Relevant JavaScript code: Code: function change(element) { if (element.id == "news" && element.src.substring(3,5) != "../") { element.src = "../images/news.png"; alert(element.src.substring(3,5)); } else if (element.id == "news" && element.src.substring(3,5) == "../") { element.src = "../../images/news.png"; } } I want the image to change to another image, but depending on the src in the HTML code. This is so that it can locate the correct folder. However, it doesn't work, and I tried using an alert statement to show me what JavaScript is reading and it seems to read "e:", which is not correct. It never reads "../", which it should. I don't know how to solve this problem, any ideas? The question is Allow the search function to start anywhere within the name, rather than just at the start (so searching for "icha" would match "Richard". can any one help? Reply With Quote 01-14-2015, 05:08 PM #2 Philip M View Profile View Forum Posts Supreme Master coder! Join Date Jun 2002 Location London, England Posts 18,371 Thanks 204 Thanked 2,573 Times in 2,551 Posts Code: <script type = "text/javascript"> var names = ["Brian", "Peter", "Richard","Thomas"]; for (var i =0; i<names.length; i++) { var str = names[i]; var n = str.search(/icha/i); // the i flag means case insensitive if (n != -1) { // match found alert (i + " " + n); // 2 1 that is, the match is found at the third array item at the second character } } </script> Or:- Code: <script type = "text/javascript"> var names = ["Brian", "Peter", "Richard","Thomas"]; var pattern = "icha"; for (var i =0; i<names.length; i++) { var str = names[i]; var s = new RegExp(pattern,'i').test(str); // the i flag means case insensitive if (s) { alert ("Match found at array index " + i); // 2 } } </script> All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit. I want to have a simple code such that some data is stored in array. When we create a search box it has to give suggestions from the data stored in array. EDIT: Great...I already screwed up the title. It's not a div class but anchor class :s I've got previous programming experience but unfortunately none with javascript. I was hoping to get Fancybox working on my website. It actually is already but there's a small problem with dynamic varibles I haven't been able to resolve. I've described the problem in the comment line in the code section. Must be a really simple solution, after all I'm a complete js noob. Note that I've simplified the code by removing parts unnecessary in understanding what I'm after. Code: <html> <head> <script type="text/javascript"> $(document).ready(function() { var title1 = "This is title no 1", title2 = "This is title no 2", title3 = "This is title no 3"; $("a#example_video").click(function() { $.fancybox({ 'title' : 'title' + this.className.replace("video", "") // "title" should equal to "title1", "title2" or "title3" depending on which link a user has clicked. How to accomplish this? </script> </head> <body> <a class="video1" id="example_video" href= .....>Video1</a> <a class="video2" id="example_video" href= .....>Video2</a> <a class="video3" id="example_video" href= .....>Video3</a> </body> </html> Also the only reason for using classnames on anchors is to be able to distinguish between different video clips. Is there a better method I should use? Hello everybody I'm working on a tiny personal project, that only needs a little push to be finished. As I'm not a skilled enough scripter, I had to google around, but without any result exactly same as mine. What I'd like to know, how can I search in the body for strings surrounded by other strings that signs that the string I want is between them? example: Code: I'm a part of the body :3 // <- not surrounded part of the body [cust]You want me in your array :)[/cust] // <- this one is surrounded I won't get into your array :( // <- this one is not, just like the most of the body I have no snippet that could help me. Thank you in advance I have the following code as shown below: Code: <html> <script type="text/javascript"> var all = []; var a = ["1234", "Jim", "Lab1", "5455"]; var b = ["1235", "Jack", "Lab1", "5459"]; var c = ["1236", "Jane", "Lab1", "5455"]; var d = ["1237", "June", "Lab1", "5458"]; var e = ["1238", "Jill", "Lab2", "5461"]; var f = ["1239", "John", "Lab2", "5462"]; var g = ["1240", "Jacab", "Lab3", "5465"]; all.push(a); all.push(b); all.push(c); all.push(d); all.push(e); all.push(f); all.push(g); for(var i=0; i<all.length; i++){ document.write(all[i] + "<br>"); } </script> </html> How to I get the unique & in order of column 4 given that column 3 is given. Example: If the user provide the value 'Lab1' for column 3, the Javascript will return me the following? Code: "Lab1", "5455" "Lab1", "5458" "Lab1", "5459" In this case, Let's take Google Search as example: The code is JScript .NET, which is basically a .NET version of Javascript. Regardless of language, Anyone with appending type of skill can answer my question. This code is used in Fiddler(It's a Man-in-the-middle proxy) Code: if (oSession.uriContains("&q=")) // oSession is a Fiddler object session // uriContains() function, checks for case-insensitive string from the URI { var str = oSession.fullUrl; var sAppend = "test1+test2+test3"; if (!oSession.uriContains(sAppend)) { oSession.fullUrl = str.replace( "&q=","&q="+sAppend); } } For those who are confused, It says, If &q= is present in the URI, replace/append &q= with &q=test1+test2+test3 Problem: It appends test1+test2+test3 instantly, when it sees &q= in the URL. Basically, how do I make it wait until I click the submit/search button Thank you. Update: I heard about Onsubmit() event, but not really familiar with it. How do I use it? like, should I go to google source page and edit the form id? Also, Any other methods besides Onsubmit()? Hi Everyone! I have a website that I'm designing where I have the need to search multiple sites at specific times. By this I mean that In some cases, we would want to search only the internet using google, or only search the site that I've created (which currently uses the jse_search.js solution), or only our company's website. I currently have four different search boxes that will search either the internet, the internal site, a separate internal site, or a third-party website, which all working fine. The problem is that the search boxes take up quite a bit of space, and the layout is becoming cumbersome. Is there a way in Javascript I could use a single search box and a drop-down list to select which method to use? The code I'm currently using is below. With the exception of the Google search function, I've modified some of the site names to general site names and paths to preserve the company's anonymity: Code in the <head> tag: Code: <script language="JavaScript1.3" type="text/javascript" src="jse_form.js"> </script> Code in the <body> tag: Code: <!--Begin Internal Site Search 1!--> <div> <p style="text-align: center;"> <table border="0" cellpadding="0"> <tr><td><form name="jse_Form" onsubmit="search_form(jse_Form);return false"> <input type="text" name="d" size="30"> </tr></td> <tr><td> <input type="button" value="Internal Site Search 1" onclick="search_form(jse_Form)"> </form> </tr></td> </table> <!--End Internal Site Search 1!--> <!--Begin Internal Site Search 2!--> <div> <p style="text-align: center;"> <table border="0" cellpadding="0"> <tr><td> <!--webbot bot="Search" S-Index="all" S-Fields S-Text="Search for:" I-Size="20" S-Submit="Start Search" S-Clear="Reset" S-TimestampFormat="%m/%d/%Y" TAG="BODY" b-useindexserver="1" startspan --> <form action="http://sitesearch2.idq" method="POST"><input type="text" name="UserRestriction" size="30" value> </tr></td> <tr><td style="text-align: center;"> <input type="submit" value="Internal Site Search 2"></form> </form> <!--webbot bot="Search" i-checksum="4210" endspan --> </td></tr> </table> </div> <!--End Internal Site Search!--> <!--Begin Google Search!--> <form method="get" action="http://www.google.com/search"> <div> <p style="text-align: center;"> <table border="0" cellpadding="0"> <tr><td> <input type="text" name="q" size="30" maxlength="233" value="" /> </tr></td> <tr><td align="center"> <input type="submit" value="Google Search" /></td></tr> </table> </div> </form> <!--End Google Search!--> <!--Begin Third Party Search!--> <form id="keywordSearchForm" method="get" action="http://www.site3.html"> <div> <p style="text-align: center;"> <table border="0" cellpadding="0"> <tr><td> <input class="input" type="text" name="keyword" size="30" /> </tr></td> <tr><td align="center"> <input type="hidden" name="origin" value="keywordsearch" /><input id="go" class="button" tabindex="0" type="submit" value="Third Party Search" /> </td></tr> </table> </div> </form> <!--End Third Party Site Search!--> Hi experts, is it possible via Javascript to search certain websites with certain keywords without having to use specific search engines? example search only the following: 1. www.yyy.com 2. www.aaa.com 3. www.zzz.com for the keyword "Laminat" and open the sites accordingly. thx Not sure where the best place to post this would be...but what would be the best way (if at all) to go about writing a program that would automate data entry (from an excel spreadsheet) into an online database. I've been doing them one by one for the past couple of weeks...only have about 10,000 left...any help would be much appreciated thanks I'm working on a data entry form for an application, and I'm stuck at what may end up being some bad code. I'm capturing data for two tables (custinfo and orderinfo) on one page. The top part of the data entry form collects data once for the transaction, kind of like an invoice captures customer details. The bottom part (the part I'm stuck on) captures order information line after line. So, for example: TopForm (custinfo) Customer name Customer address Customer city, state, zip Bottom form (orderinfo) Item description Item quantity Item price What I'm looking for is two things: 1) The ability to extend the bottom form line by line as the order grows. 2) The ability to remove liness (last in, first out) from the bottom form if the order needs to shrink. I have a script that needs extra variables added, but need some help on how to do it. I need a textbox to add shipping charges, and also a text box to add sales tax. Then these 2 amounts need to be added to the grand total. Any help appreciated. The link below is to the page that has the form and code: http://spectrum-enterprise.com/sm/smorder.html I'm making 2 sites and need a visitor who lands on the home page of either site to first be shown a page that shows a company introduction and 2 links at the bottom so they can choose which division of the site (which website) they want to go to. Does anyone know of any pre-made java or other code that would allow for this? So that a visitor doesn't get stuck in a cycle of being taken from one home page to the other and having the pop-up every time, I'm thinking it will likely need to write a cookie saying they've seen the pop up, then it won't appear again. Each site would then have to check for a cookie from either site. Does anyone have any ideas on how to make this work? I'm open to anything and have to have it done by friday Hi, I am trying to make an entry confirmation form. This is going to be used as a disclaimer for my website. How can I make this to where it only shows on the main page and not every page a user goes to on my website? <script> var enter=confirm("examples insert text") if (!enter) window.location="http://www.google.com" </script> Now I cannot get this code working. At the moment it needs to just update the table with the message and the user id ($uid) but it will not, the status does not return as "200", which the IF statment checks. AJAX: Code: function sendMessage() { if (sendReq.readyState == 4 || sendReq.readyState == 0) { var url = "scripts/get_data.php?uid=<?=$uid?>"; sendReq.open("POST", url, true); sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sendReq.onreadystatechange = sendReset; var param = 'message=' + document.getElementById('txt_message').value; param += '&chat=1'; sendReq.send(param); if (sendReq == 4 || sendReq == 200) { document.getElementById('txt_message').value = ''; } } } PHP Code: //Send message if (isset($_POST['message']) && $_POST['message'] != '') { //Variables $chat = mysql_real_escape_string($_GET['chat']); $message = mysql_real_escape_string($_GET['message']); $query = "INSERT INTO message (chat_id, post_uid, recipient_uid, message, post_time) VALUES (1, ".$sid.", 4, ".$message.", date)"; mysql_query($query); } And here is the form if you want to see it: Code: <form id="frmmain" name="frmmain" onsubmit=""> <input type="button" name="btn_get_message" value="Refresh Chat" onclick="getMessage()" /> <input type="button" name="btn_reset_chat" id="btn_reset_chat" value="Reset Chat" onclick="resetChat()" /> <br /> <input type="text" id="txt_message" name="txt_message" style="width: 447px; color:#000000;" /> <input type="button" name="btn_send_message" id="btn_send_message" value="Send" onclick="sendMessage()" /> </form> I am using an example of this kind of feature to reference to, and I don't understand how the param is being sent to the server, is it being added to the URL? URL before param: scripts/get_data.php?uid=3 after param has been sent (changes in dark orange): scripts/get_data.php?uid=3 message=message goes here&chat=1 If so then I am guessing that the var param should look like this(change in red): Code: var param = ' & message=' + document.getElementById('txt_message').value; param += '&chat=1'; sendReq.send(param); I'm a fairly new ColdFusion developer and a true Javascript newbie, so gentleness please! I have a CF application that accepts two dates. The first date - Out Date - is required. The second date - Return Date - is optional, but if populated, must be equal to or greater than the Out Date. Here's the screen & code: <script type="text/javascript"> function compareDate() { if(document.editleave.temp_ret_date.value != "01/01/0001") if(document.editleave.temp_ret_date.value < document.editleave.temp_out_date.value) alert("Scheduled Through Date cannot be earlier than the Scheduled Out Date!"); setFormFocus(document.editleave.temp_ret_date); } </script> <cfinput type="datefield" name="temp_out_date" id="temp_out_date" value="#DateFormat(Now(), 'mm/dd/yyyy')#" validate="date" ValidateAt="onBlur,onSubmit" message="Please enter a valid date in MM/DD/CCYY format." size="6" maxlength="10"> <cfinput type="datefield" name="temp_ret_date" id="temp_ret_date" value="01/01/0001" ValidateAt="onBlur,onSubmit" message="Please enter a valid date in MM/DD/CCYY format." onBlur="compareDate();" size="6" maxlength="10"> Everything works fine....EXCEPT...if the Return Date is invalid, I cannot click on the calendar box to select a date (or open another browser session). Basically, IE is locked up until the user manually types in a valid date. This ain't good. I want to be able to trap the error, just like I have, but I still want the calendar selection box to be usable. Is it possible to "reset" the error condition after the intial warning to only trigger on the onSubmit parameter? |