JavaScript - Replacing Substrings Query
Hi,
I know how to replace all the + signs in a url string by spaces, like this: newstring=oldstring.replace(/[+]/g," "); but how do I replace a substring by a comma easily? This doesn't do it: newstring=oldstring.replace(/[&menuitems]/,","); Thanks in advance for any help. Similar TutorialsI am new here. I've been trying to learn javascript, php, ajax, and others on my own, through the help of researching. I can't seem to manage figuring this out. I'm using a string "A B C" for sake of example. Variable d goes through a for loop checking for capital letters, to make substrings. d=0 finds A and makes the substring A. d=1 finds B and makes the substring AB. d=2 finds C and makes the substring ABC. It seems to me that: d=0 should be A d=1 should be B d=2 should be C. (which is what I want) Here's the actual code (obviously, the actual string being worked with is not 'A B C'): Code: response = response.replace(/([A-Z])(?![A-Z])/g, " $1") ; response1 = new Array(); caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; a=0 ; q=0 ; for (a=0 ; a<=response.length-1 ; a++) { for (q=0 ; q<caps.length ; q++) { if (response.charAt(a) == caps.charAt(q)) { response1[response1.length] = response.substring(a,' ') ; }}} response1 = response1.filter(function(){ return true}) ; b=1 ; for (b=1 ; b<=response1.length-1 ; b++) { document.write(response1[b] + "<br />") ; } } I'm new at JavaScript and am trying to figure out a simple text calculator as seen on a science project website. The project says the final program should calculate: 1. the number of sentences contained in the text, 2. the number of words in each sentence, 3. the number of letters in each word, 4. the average number of words per sentence, and 5. the average word length. I have pretty much everything (I think) but am being completely stumped by item number 2 and item number 4. The code is below. Can anyone help me understand what I should be doing for point number 2 and 4? I understand how to calculate and display the length of an item in an array and return it's values as 5,6,7,8 etc where the value is the length of the word, but i can't grasp how to calculate the number of items in an array to read 2,5,9 where the values are the number of words per each sentence...so confused! Below is my code...apoligies if it's sloppy...it's my first javascript code experience 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>Count</title> Code: Is it possible, when a page is reloaded, to replace a link to a javascript in the head with a link to a different script?
hi, I am curious about the use of appendChild() here... I have a solution that does what I want it to do, like this: Code: var sidebarEntry = createSidebarEntry(poly, html); sidebar.appendChild(sidebarEntry); sidebar.replaceChild((sidebarEntry),sidebar.firstChild); but it seems to be going the long way around. Not that it matters much (I guess), but all I want to do is to add an item to the sidebar and when a new item is added, remove the existing one. I tried just using relaceChild() on its own, but the code seems to need something to be replace... Is the above the best way to do it? Or is it possible to set the child to some dummy position like null to begin with, so that the code is not constantly reloading and replacing items (which is what I understand is going on now)? here's the page I'm working on, if you want to see the rest of the code. I appreciate any thoughts. I have the following code that works fine: Code 1 Code: <script type="text/javascript"> AJS.AEV(window, 'load', function() { GB_show("Special Presentation", "http://www.site.net/temp/images/ads/special.jpg", 352, 600); }); </script> I also have the following code that works fine: Code 2 Code: function DoTheCookieStuff() { visited=getCookie('visited'); if (visited==null) { setCookie('visited','yes',365) MyWindow=window.open('http://www.site.com','MyWindow','toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=640,height=480,left=1,top=1'); } } However, I want to replace the blue code in the Code 2 immediately above that opens the window with the red code in the Code 1 section that opens the GreyBox window. I tried the following but it's just not working: Code: function DoTheCookieStuff() { visited=getCookie('visited'); if (visited==null) { setCookie('visited','yes',365) AJS.AEV(window, 'load', function() { GB_show("Special Presentation", "http://www.site.net/temp/images/ads/special.jpg", 352, 600); }); } } Thank you. I'm sorry this is simple, but: Code: function printInput(f){ var ifr = window.frames['printFrame']; var v1=readCookie('lesson'); if (v1==1){v2='WORKNOTEPAD1'} if (v1==2){v2='WORKNOTEPAD2'} if (ifr){ ifr.document.getElementById('content').innerHTML=f. v2 .value.replace(/\n/g,'<br />'); ifr.focus(); ifr.print(); } else { something } } In this line: Code: ifr.document.getElementById('content').innerHTML=f. v2 .value.replace(/\n/g,'<br />'); How can I make it stop erroring and act as if the "v2" were, say, "WORKNOTEPAD2"? If the line contains the string it all works fine. It must be so simple... Hi, I am quite new to JS and is currently building a website with a little of JS Function. Ok, so I have a Page tat consists of 10 images. Each images when on mouse over, will display a larger version on the images, using JS(onmouseover). Then, I will have a next button that will replace all the images, using Ajax, which means my page will not be reloaded. The problem comes here when I have to replace all the onmouseover effect. I can replace all the thumbnails without problem, but the onmouseover I have problem replacing. Is there anyway that I can dynamically replace all the onmouseover? Any help will be appreciated. Thanks Hey guys... Basically I need a function that will remove numerous elements from a page based on their class. I then also need a function to bring those elements back afterwards. If anyone is wondering, I'm trying to add buttons to remove/add images. Thanks! I have 16 links on a page, when each link is clicked, I need to change text that is on the page. This is Page 1 link 1 This Is Page 1 link 2 This is Page 2 ... link 16 This is Page 16 How can I do this? Excuse my ignorance, this is my first JS project Hi Guys/Gals I am trying to do a small website for our family to keep track of each other's home address and etc.. using asp for the codes .. (its been ages since i indulge myself) i have 2 forms in which in FormA, i will key in the following address 666, ST Avenue #03-09 Anchor View Residence 123456 when i click on submit it shows this on FormB 666, ST Avenue #03-09 Anchor View Residence 123456 my question is how do i get it to be shown as 666, ST Avenue #03-09 Anchor View Residence 123456 in FormB in my formB.asp, for the particular code, all i wrote was Code: {Form.address} I am in need of advise, have been stuck here for the past 1 day, not knwing wat to start or how to start. Thank you Hey folks. I currently have a PTZ camera that is taking screenshots every minute (or so) and sending a copy of that image (overwriting the previous one) to my website. Basically, the visitors to the website will be greeted with a live screenshot. The problem is if the visitor hits the website at just the right time, the screenshot won't be loaded completely while the 2nd most screenshot is being overwritten... creating an empty image load (the dreaded red X). What I am looking to do with Javascript is to be able to load a generic image in the scenario in which no image loads. Is that possible? I tried doing something like the following and onload-ing it, but it wouldn't work. Any help would be appreciated. Thanks! Code: <script type="text/javascript"> <!-- function replaceSrc() { var images = document.getElementsByTagName('img'); for(var i = 0; i < images.length; i++) { var img = images[i]; if(img.src.length == 0) { img.src = 'images/garbage.gif'; } } } --> </script> Hi. I have the following line in my HTML file <table class="bz_buglist" cellspacing="0" cellpadding="4" width="100%"> And I need to use javascript to change this line to <table id="main_table" class="bz_buglist" cellspacing="0" cellpadding="4" width="100%"> . I just need to add the ID part. How can I do this? Is there a method of replacing the line, or a way to "add attribute"? thanks! kev. Ok, i have a javascript that creates a table then appends it to an empty div. This works fine in FF, however it fails to work in IE8. [edit] I have decided to post the entire code since I wasn't having any luck getting any suggestions. Maybe now someone can help me out. Code: function createTable() { var machine = document.Catalog.machine.value; var dSearch = document.Catalog.dSearch.value; var section = document.Catalog.section.value; var x = xmlDoc.getElementsByTagName('record'); var y = xmlDoc.getElementsByTagName('data'); var newEl = document.createElement('TABLE'); newEl.className = 'resultsTab'; var tmp = document.createElement('TBODY'); newEl.appendChild(tmp); var row = document.createElement('TR'); var n = 1; var headings = new Array("Col1", "Col2", "Col3", "Col4", "Col5"); var dcount = 0; //for (t=0;t<10;t++) { for (t=0;t<y[0].childNodes.length;t++) { if (y[0].childNodes[t].tagName == "record") { // it is required for FF to work properly dcount++; } } if (document.Catalog.thumbImg[0].checked) { var container = document.createElement('TH'); var theData = document.createTextNode("Pic"); container.className = "topline1"; container.appendChild(theData); row.appendChild(container); } for (h=0;h<headings.length;h++) { var container = document.createElement('TH'); var theData = document.createTextNode(headings[h]); container.className = 'topline' + n; container.appendChild(theData); row.appendChild(container); n++; } tmp.appendChild(row); switch (searchtype) { case 1: var objRegex = new RegExp(machine); break; case 2: var objRegex = new RegExp(section, "i"); break; case 3: var objRegex = new RegExp(dSearch, "i"); break; default: document.getElementById("replaceTxt").innerHTML = "<p>There was an error processing your request.</p>"; } var rowColor = 1; var noResult = "false"; for (i=0;i<dcount;i++) { var row = document.createElement('TR'); var searcharr = new Array(); for (j=0;j<x[i].childNodes.length;j++) { if (x[i].childNodes[j].nodeType != 1) continue; searcharr.push(x[i].childNodes[j].firstChild.nodeValue); } var searchstrg = searcharr.toString(); if (searchstrg.match(objRegex)) { var t; var m=1; if (document.Catalog.thumbImg[0].checked) { var pn = "images/"+searcharr[2]+".jpg"; var container = document.createElement('TD'); var refLink = document.createElement("A"); refLink.setAttribute('href', "javascript:viewImg(\""+pn+"\")"); var imgHolder = document.createElement("IMG"); imgHolder.setAttribute("src", pn); imgHolder.style.height = "75px"; refLink.appendChild(imgHolder); container.appendChild(refLink); container.className = "img"+rowColor; row.appendChild(container); } for (t=0; t<searcharr.length; t++) { container = document.createElement('TD'); var theData = document.createTextNode(searcharr[t]); container.appendChild(theData); container.className = 'line' + rowColor + '_' + m; row.appendChild(container); m++; } tmp.appendChild(row); rowColor == 1 ? rowColor++ : rowColor--; document.getElementById("replaceTxt").innerHTML = ""; document.getElementById("replaceTxt").appendChild(newEl); noResult = "true"; } } if (noResult=="false") { document.getElementById("replaceTxt").innerHTML = "<h3>Sorry, there was no match to your search. Please try again</h3>"; } } xmlDoc is the xml object from my import xml function. it is working correctly. IE does not give any JS errors, it just does nothing. If I put text in the Code: document.getElementById("replaceTxt").innerHTML field, IE displays the text in the correct spot so I know the code is working. I also verified the data is being written correctly to "newEl" via IE 8 debugger. IE just does not want to append the newEl child to the <div> tag. This is where I assume the problem is. I'm a bit of a Javascript noob, so please bear with me! I'm trying to create a piece of code that will check a webpage and replace any instances of a specific line of HTML with another line of HTML. The code I have so far doesn't work, but I have a feeling that's down to my awful Javascript skills! For example, in the following code I'd want to replace all instances of <li><a href="index.pdf">Index</a></li> with <li><a href="contents.pdf">Contents</a></li> Code: <div id="downloads"> <p>Please click on your downloads below:</p> <ul> <li><a href="index.pdf">Index</a></li> <li><a href="chapter1.pdf">Chapter 1</a></li> <li><a href="chapter2.pdf">Chapter 2</a></li> <li><a href="chapter3.pdf">Chapter 3</a></li> <li><a href="credits.pdf">Credits</a></li> </ul> </div> The Javascript I have so far is: Code: <script type="text/javascript"> var aEls = document.getElementsByTagName('a'); for (var i = 0, aEl; aEl = aEls[i]; i++) { aEl.href = aEl.href.replace('index.pdf','contents.pdf'); } Obviously this is meant to just replaces the a href elements, but I got a bit stuck after this! Any pointers would be much appriciated. Code: var bakaBanner; bakaBanner = document.getElementById('head'); if (bakaBanner) { var str=/\/images\/banners\/[A-Za-z0-9_]+.[a-z]+/; document.write(str.replace(/\/images\/banners\/[A-Za-z0-9_]+.[a-z]+/, "http://img218.imageshack.us/img218/5904/revyblacklagoon1.png")); } Alright, here's the deal... This is a script to be used in Greasemonkey. The page this is for changes their banner every 5 minutes, the name changes and it could be either jpg or png. I'm fairly certain my regex itself: \/images\/banners\/[A-Za-z0-9_]+.[a-z]+ Is correct... Here's an example of what it's looking through: Code: <div id="head" style="background-image: url(/images/banners/kiminitodoke.jpg);" > <div id="header_left"> <div id="rss"> My guess is that it has something to do with this line: document.write(str.replace(/\/images\/banners\/[A-Za-z0-9_]+.[a-z]+/, "http://img218.imageshack.us/img218/5904/revyblacklagoon1.png")); I'm new with Javascript, so I don't really understand why it's not working... :/ Apologies for the confusing title. I have a scipt Code: ....style: google.maps.NavigationControlStyle.SMALL } }); <? $query = mysql_query("SELECT * FROM `family_support` WHERE 1"); while ($row = mysql_fetch_array($query)){ $org_name=$row['org_name']; $lat=$row['lat']; $long=$row['long']; $org_address=$row['org_address']; echo ("addMarker($lat, $long,'<b>$org_name</b><br/>$org_address');\n"); } ?> center = bounds.getCenter(); map.fitBounds(bounds); } </script> I want the $query to be the same as another query set up further down the page; Code: $sql_result= "SELECT * FROM family_support WHERE org_name LIKE '%$org_name%'"; if ($area != 'All') { $sql_result .=" AND area LIKE '%$area%'"; } if ($services != 'All') { $sql_result .=" AND services LIKE '%$services%'"; } if ($hardiker != 'All') { $sql_result .=" AND hardiker = '$hardiker'"; } My php is dire and help with putting these together would help me display markers on a map for all results from a search form rather than display all records within the db as is happening at present.... Also would I need to move the script from the head tag? My problem is the following. I have an editable <div>: <div class='edit' id='divContent' name='divContent' ContentEditable>some text</div> If I press 'enter' two lines are skipped. I know that if I press 'shift+enter' only one line will be skipped. If I check the unformatted code of the contenteditable div, 'enter' gives me <p> and 'shift+enter' gives me <br>, which makes sense because <p> is formatted differently than <br>. So I want to change 'enter' into 'shift+enter' I have already found something to capture and kill 'return' function killReturn() { key = event.keyCode; if (key == 13) return false; } But now for some means to replace the killed 'enter' with 'shift+enter'? Thanks a lot in advance for any advise! If you have a java script function in page loading to execute a code to affect a control on the form, but that particular control is rendering in page init event and when you test the page, you find it gives you an error message “object name ‘control name’ is not exist”, because of scripting code executing before form element rendering? What should you do to fix that? Thanks Waqar |