JavaScript - Modify String Without Creating A New Variable
Is this possible?
Code: String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g, ""); } var myStr = " Hi what's up? "; myStr.trim(); alert("{" + myStr + "}"); //will result in the original string, not the trimmed version It's possible with numbers: Code: var myNum = 10; myNum++; alert(myNum); //11 Similar TutorialsI would like to apologize in advance if my question sounds too simple. I am a novice to Javascript and have not had any real formal training here. I am creating a webpage which utilizes a separate .xml file to populate values in my webpage. This is my webpage (links.html): and here is my xml file (links_data.xml): This is my output: I have a link at the bottom of the page that points to the device being displayed. In this case, the link should be: http://10.90.139.201 If my xml file changes, it should point to the new IP address. My question is, how do I get the link to work? Currently, my current link (<a href="http://"IP_address"">Link</a>) does not populate any information. Appreciate any guidance here... thanks. I am working on a javascript for my blackberry. I am trying to capture the latitude and longitude of that phone. I am able to get the coordinates in an alert box but am having a little trouble writing it to a form input field. Can anyone help me with this? Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>GPS Testing</title> </head> <body> <script type="text/javascript"> if(blackberry.location.GPSSupported){ document.write("Your device supports GPS locating"); blackberry.location.onLocationUpdate(window.alert("Latitude: " + blackberry.location.latitude + " Longitude: " + blackberry.location.longitude)); var lat = blackberry.location.latitude(); var lng = blackberry.location.longitude(); blackberry.location.refreshLocation(); document.write(lat); } </script> </body> </html> I am only able to test with Blackberry. I was trying to create a couple of variables with the "var lat =" and var lng =". With this script I get the alert window and when I click ok it writes "Your device supports ..." Thanks for any help! I'm using a form data reference, something like P-1234 for example to create a text file and a link to the file. This is during the construction of a new table or table row with javascript. I have an array of one or more references submitted via form using $_POST. On my action page I am creating a txt file P-1234. If I am creating a table ot table row using createElement(), one of the cells will have a link to the file. If the file is created as follows: PHP Code: $File = $_POST['ref'][$i] . "txt"; After creating the cell Code: var Cell = document.createElement('td'); I assign the file to a variable var File = "<?= $File ?> "; I assume the link is inserted using innerHTML? If so, do I just append the filename to the end of the file path like this? Cell.innerHTML = "http://localhost/Project/+File"; If this is not the right way can someone point me in the right direction? Code: var x = "blahblah".indexOf("blahblah"); if (x !== -1){ // do something with x alert("Present"); } else { alert("Not present"); } Is there some syntax/code structure that allows something like this: Code: if ( (var x = "blahblah".indexOf("blahblah")) !== -1){ // do something with x alert("Present"); } else { alert("Not present"); } Very new to javascript, so I apologize in advance. I honestly tried to figure this out on my own, and I'm just not able. I'm trying to create a family tree online that has several other related family charts. I want each person's name and birth year to be stored in javascript variables, then I want to be able to just go through the tree, and put the persons name once, and have it put the appropriate name and birthyear in the little boxes that the css is creating. In the js file... I would do this for each family member. Code: var johnname = "John Smith"; var johnbirthyear = "Jan 1 1930"; var janename = "Jane Doe"; var janebirthyear = "Jan 1 1940"; var mikename = "mike wilson"; var mikebirthyear = "Jan 1 1950"; var saraname = "sara johnston"; var sarabirthyear = "Jan 1 1960"; In the HTML file... Code: <div id = "boybox"> <script type="text/javascript"> id = "john"; <!-- this is the only thing I want to have to change --> name = id+"name"; document.write(name); </script> <div id = "year"> <script type="text/javascript"> birthyear = id+"year"; document.write(birthyear); </script> </div> </div> So I thought I had it all figured out. I thought that the above would take the id of John that I added, go to the line that sets the name, add john to the word name, and then document write the variable of johnname from the js file. I know you're laughing at me because of course it instead made the value of name to johnname, then wrote the word johnname in the box. Can anyone point me in the right direction please? ultimately I want to go through and be able to just type the persons name in the html for that box once, and have it update with their appropriate info. Thanks, and sorry for such a rookie question. How can I use a string to select a variable? E.g. Code: function load_menu( menu ) { //Availible menus var login = Array('Login','Register','Recovery'); var settings = Array('Profile','Account','something'); //Menu container ID's var cons = Array('menuitem1','menuitem2','menuitem3'); for ( var i = 0; i < cons.length; i++ ) { document.getElementById(cons[i]).innerHTML = menu[i] ; alert(menu[i]); } } "menu" is "login". menu[i] should reference the "login" variable and then put one of the values for the login variable array in its place. If you understand what I mean? So if menu is settings then the settings array would be used. Hi all, I am having a hard time on this, can you help pls? I have this code? $("select[name=objeto"+contcheck+"]").change(function(){ $("select[name=titulo"+contcheck+"]").html('<option value="0">Carregando...</option>'); where contcheck is a counter, so a number. It does not work at all, any help how to concatenate string and variable like this: objeto1 objeto2 objeto3 .... and so on. Thx. I am making a function where I need to know whether the input is a variable with a string value var foo = "bar"; Function(foo) or if it is a string, wherein it would be the name of an object object = { ThingsInside: function(){code} } Function('object') Code: function Function (object){ if(object is a string){ do this } else if (object is a variable with a string value){ do this } else{ do something else } Because they would be both passed by value, I'm not sure how or if there is a way to tell as they would both just appear as string inputs, right? Say I had a bunch of elements with id names "id1", "id2". "id3" etc. Then say I had a function that adds a click handler such that when you click these elements it gets the id name with this.attributes[1].nodeValue;. Then say I had a bunch of arrays with the same names as the ids var id1 = ["data", false, 45]; var id2 = ["otherdata", true, 15]; var id3 = ["otherotherdata", null, 65];. How would I set a variable "currentid" to the array with the corresponding name as the id name? I guess the underlying question is, how would I convert a string to a variable name?
Here is my code Code: function copy_grid(from,to) { var copy_from = from +"_24_product_cost"; // in this instance from = grid1_White var copy_to = to; var tmp_field = getElementById(copy_from).value; // ERROR: getElementById is not defined } document.product.grid1_White_24_product_cost.value // works as expected but I wanted this to be dynamic. Any ideas? I am just starting to learn javascript and was hoping someone might be able to answer my question: var months = "April Showers"; document.write('<p><img src="ad11.jpg"); document.write(" alt="); document.write(months); document.write(">"); document.write(months); How come when I display my output to a browser without the presence of the ad11.jpg file, only the first word (April) in the text string "April Showers" is dispayed where the .jpg file is suppose to be...but the last line displays the entire text string. Hi, I have a javascript program that currently takes the contents of a *.txt file and converts it to a string-type variable using a hidden iframe. For my pruposes it would be much nicer to read a *.csv file instead, however the method I'm using creates the "open/save file" pop-up window. All files are client-side and in the same folder on one computer; this is NOT server-side or internet-based. It is simply javascript used in an HTA file as programming code. The *.csv does not need to be edited, just read. I need to know how to convert the text of this *.csv file into a string variable. I have heard of using "xmlHTTPRequest (AJAX)" but am not familiar with how to use this. Any advice or code would be greatly appreciated. Hi All, Am new to Javascript but fluent in C++ and other languages, I have a query, i made a global variable to hold string information, can the value of this string be assigned to a form input field value? I mean in the 'default value sense' I am just wondering if there is any syntax to accomplish this. EG in my javascript i have the string nameVal; It is a global variable, i would like to know if it is poosible access its value for use in the line below shown with italics, or is it basically always a case of 'type the value in manually yourself' and then rely on events functions to update the field thereafter? Code: Name: <br><input type = "text" name = "nameField" size = "50" value = my string val here onChange ="this.value=storeVal(this.value, '0')"></input></br> I'm sure it's just a syntax problem. You can ignore most of this function because most of it works. The part I'm asking about is the "coordinates" variable 4th line from the bottom. The "office3" variable is a dynamically populated office number. I have the office numbers all defined as variables somewhere else but since I can't just name the variables as numbers I had to put a $ in front of them. Basically I'm trying to call the office number variable, $1656 for example. So "coordinates" is meant to be equal to "$+the chosen office number". Problem is coordinates is coming up as literally "$1656" instead of the value of $1656. Sorry if that was confusing, can anyone point out the problem? function display(name3,office3,officePH3,mobile3,email3) { var instruct="<span class='style4'><strong>"+"LOOK FOR THE FLASHING COMPASS ROSE ON THE MAP"+"</strong></span>"+"<br /><br />"+"<span class='style4'><strong>"+"HOVER ON A PRINTER TO VIEW ITS INFO OR CLICK TO MAP IT"+"</strong></span>" var info_output="NAME: "+(name3)+"<br /><br />"+"OFFICE: "+(office3)+"<br /><br />"+"DESK PHONE: "+(officePH3)+"<br /><br />"+"MOBILE: "+(mobile3)+"<br /><br />"+"EMAIL: "+"<a href=mailto:"+(email3)+">"+(email3)+"</a>"+"<br /><br />"+(instruct) document.getElementById("info").style.visibility="visible"; info_text.innerHTML=info_output document.getElementById('iframe_container').style.overflow='auto' document.getElementById('viewer').src=('Location_Files/')+(office3)+('.htm') var coordinates='$'+(office3) document.getElementById('compass_rose').style.top=('200') document.getElementById('compass_rose').style.left=('200') document.write(coordinates) } I need to declare a variable (astString) and initialise it with a string of asterisks. astString needs to be the same length as another variable (otherString). otherString changes it value regularly so I think I need to use the .length property in some way but I can't see how! This should be simple but it's eluding me completely. Hey, I'm looking for a way to get a variable from a parent window in a child window. I'm aware that you can write the variable to a hidden field and then get the field's value in the child window, but I'm looking for a cleaner (all JS preferable) way of doing this. Something like this, but I believe this is an out-dated solution: Code: var str = window.opener.str; alert(str); Any solutions? Hello, I am trying to update an iframe when the user makes a selection from a select box. I don't have any trouble with changing the iframe, but when I try to send a varible as a quey string, the variable doesn't pass to the iframe properly. // this is in my header, and it is initiated from the onchange below. // it changes the page fine, but doesn't read the varible "num" // that was passed to the function <script language="javaScript"> <!-- function updatenotes (num) { document.getElementById("desc").src = 'iframe1.cfm?dec=num' } //--> </script> // this is the select box that initiates the function from onchange // for simplicities sake, I hard coded 3 options, but in reality, // these options are ever changing <select name="Description" onchange="updatenotes(this.options[selectedIndex].value)"> <option name="">Select a project</option> <option name="2" >project2</option> <option name="3" >project3</option> </select> Any help you can offer would me ver appreciated. thanks I have this function: Code: function SelectedRadio(form, name) { for(var i = 0; i < form.name.length; i++) { if(form.name[i].checked == true) { return form.name[i].value; } } return "Error!"; } Form is a form object, and name is a string of the name of the radio button. I thought it was trying to use the variable name as the name of a field. For example: Code: function OnFormSubmit(form) { SelectedRadio(form, "fruits") } Instead of form.fruits its using form.name. Basically I am trying to use the variable as a string. Is there anyway around this? |