JavaScript - Splitting Url So I Can Use The Last Part
I'm trying to figure out how to split a url variable... I tried these two ways, with an alert to see my result, but I'm not getting anything. Suggestions?
Code: function() { myURL = "http://www.test.com/folder1/folder2/someone" var partsA = myURL.split("//"); var parts = partsA[1].split("/"); alert(parts[2]); } Code: function() { myURL = "http://www.test.com/folder1/folder2/someone" var parts = myURL.split('/'); var result = parts[parts.length-2] + '/' + parts[parts.length-1]; alert(result); } Similar TutorialsWhat would the best be to split the following string: "only(a-z,A-Z,1-9)" I want to split it into a array to look like this: array(only, a-z, A-Z, 1-9); Alright, so I'll be honest, I HAVE NO IDEA ABOUT JAVASCRIPT! So, basically, I want, when the button is clicked, for this to grab some levels from a game and do some calculations. Simple. Well... Not for me. Here is the code I have right now: Code: <script language="JavaScript" type="text/javascript"> function GrabTheStats(rsusername){ var GrabStats = ""http://hiscore.runescape.com/index_lite.ws?player="" + rsusername; var GrabStats_array = GrabStats.split(","); attLvl = expToLevel(GrabStats_array[0],false); strLvl = expToLevel(GrabStats_array[1],false); defLvl = expToLevelGrabStats_array[2],false); hpLvl = expToLevel(GrabStats_array[3],true); magLvl = expToLevel(GrabStats_array[4],false); ranLvl = expToLevel(GrabStats_array[5],false); praLvl = expToLevel(GrabStats_array[6],false); sumLvl = expToLevel(GrabStats_array[7],false); calcResult = calcCombat(attLvl,strLvl,defLvl,hpLvl,magLvl,ranLvl,praLvl,sumLvl); document.getElementById('outputlocation').value = calcResult[0] + "/" + calcResult[1]; } </script> Code: <input type="text" name="outputlocation" id="outputlocation"/> <input type="button" onclick="GrabTheStats("zezima");" value="CLICK ME" /> Could someone help me to get this working? The calcCombat calculates things bases on the levels it grabs and produces an array of results, calcResult[0] and [1] being the two results I want right now. An example output will hopefully be "95/98". Please don't criticize, since this is for 'crappy old RuneScape'. It really doesn't help. Thanks! I am really new to JavaScript, and was following a tutorial that was using everything inline (probaby as its less confusing for the noob). Now I want to split it off into a script area, like my CSS has been. I have: Code: <SPAN onclick="document.getElementById('glance').style.display='block';document.getElementById('front').style.display='none';"> Is something like this valid (this is a guess BTW)? Code: <SCRIPT type="text/javascript"> function getElementById('glance') { .style.display='block'; } function getElementById('front') { .style.display='none'; } </SCRIPT> <SPAN onclick="document.getElementById('glance'); document.getElementById('front')"> ... </SPAN> ... or should function be document in the script area and document not be present in the SPAN attribute? ... Better yet, is there a way to put both the 'glance' and 'front' together like this: Code: <SCRIPT type="text/javascript"> function getElementById ('glance') { .style.display='block'; getElementById ('front') { .style.display='none'; } } </SCRIPT> Hello, How would I seperate a text string such that it would appear on seperate lines ie. Initial Input: ------------------------------------------- StrMsg = "This is an example of a string that will appear on seperate lines" "Hoping that this fully works, there will be no errors and all will be well" "Thank you to the experts and pros on this forum who are here to help" "This is the last line of text." Expected Outcome: ------------------------------------------- This is an example of a string that will appear on seperate lines Hoping that this fully works, there will be no errors and all will be well Thank you to the experts and pros on this forum who are here to help This is the last line of text. Much thanks and appreciation for all your help J I am trying to split the (id) values between seperate forms and the submit buttons are not located between the form tags. Note: ->The 3 forms are located at the top of page ->the (3) Submit buttons are in a query and are outside the form tags. ->In this case it does not make sense to have the form duplicating itself in the sql_query. need the correct code for the 3 script functions var form1, form2, form3 then i need the correct function submit code something like -> onClick='Split(this.form1,1)' Also: To keep this simple could the 3 scripts be combined into one? Code: <!-- SPLIT VALUES BETWEEN SEPERATE FORMS --> <script type="text/javascript"> function Add(sel,first) { if (sel.selectedIndex == 0 ) return; var temp = sel.id.split(","); sel.form1["F"+first].value = temp[0]; sel.form1["F"+(first+1)].value = temp[1]; } </script> <script type="text/javascript"> function Edit(sel,first) { if (sel.selectedIndex == 0 ) return; var temp = sel.id.split(","); sel.form2["F"+first].value = temp[0]; sel.form2["F"+(first+1)].value = temp[1]; } </script> <script type="text/javascript"> function Delete(sel,first) { if (sel.selectedIndex == 0 ) return; var temp = sel.id.split(","); sel.form3"F"+first].value = temp[0]; sel.form3["F"+(first+1)].value = temp[1]; } </script> <form method='post' name='form1' action='record_add.php'> <input type='hidden' id='F1' name='t101' value='' > <input type='hidden' id='F2' name='t102' value='' > </form> <form method='post' name='form2' action='record_edit.php'> <input type='hidden' id='F3' name='t103' value='' > <input type='hidden' id='F4' name='t104' value='' > </form> <form method='post' name='form3' action='record_delete.php'> <input type='hidden' id='F5' name='t103' value='' > <input type='hidden' id='F6' name='t106' value='' > </form> <!-- NOTE: THE SUBMIT BUTTONS ARE IN A QUERY OUTSIDE THE FORM TAGS AND NEED TO TARGET THE PROPER FORM1,2,3 onClick --> <button type='submit' id='$mytable,$columns[Field]' onClick='Split(this.form1,1)'> <button type='submit' id='$mytable,$columns[Field]' onClick='Split(this.form2,3)'> <button type='submit' id='$mytable,$columns[Field]' onClick='Split(this.form3,5)'> I got some updates on my progress... still no results. (quoting my previous thread) Quote: I need desperate help with a homework! Here's the task: First of all I need to define an array of 3 elements. Each element is associated with a different first name. Then I shall realize a first function to add a name in the table. The function takes as parameter the table previously created and return it containing an additional element whose value is the name that the visitor wants to add. I must use the dialog box prompt so that the user can specify the name he wishes to add in the table. I shall also perform a second function which will display the name that is included in the different array elements. This function will also take the table in parameters and proceed with setting the display name with a dialog box alert type that contains all of the name separated by ";". Here's what I've done so far: Code: <html> <head> <title> Task #4 </title> </head> <body> <script language="javascript"> function addName() { var names=new Array(3); names[0]="John"; names[1]="Patrik"; names[2]="Jane"; document.open(); document.write(names[3].join()); document.close(); if(names==new Array); { prompt("What name do you want to add?"+""); } return addName } </script> <noscript> <h1> You're browser doesn't support Javascript </h1> </noscript> </body> </html> Updates: Code: <html> <head> <title> Task #4 </title> <script language="javascript"> function addName() { var names=new Array(3); names[0]="John","Brian"; names[1]="Patrik","Gene"; names[2]="Jane","Sophie"; document.open(); document.write(names.join(";")); document.close(); if(names==new Array); { var addToArray=prompt("What name do you want to add?","") names.push(addToArray) } } function displayNames(addName) { alert(addName); } </script> <noscript> <h1> You're browser doesn't support Javascript </h1> </noscript> </head> <script language="javascript"> <body onLoad="displayNames(addName)"> </body> </script> </html> css: Code: #nav{ width: 960px; margin: 0 auto; } ul li{ float: left; margin-right:50px; list-style: none; } .sub-menu{ display: none; } jquery code: Code: $(function(){ ("#nav ul li").hover(function() { (".sub-menu").css("display","block"); }); html code: Code: <div id="nav"> <div class="left"></div> <div class="right"></div> <ul> <li><a href="/" class="current">Home</a></li> <li><a href="/cpanel.html" class="">cPanel</a> <div class="sub-menu"> <h2>test</h2> <p> <a href="#">test</a> <a href="#">test</a> <a href="#">test<</a> </p> <p> <a href="#">test</a> <a href="#">test</a> <a href="#">test<</a> </p> </div> i am sorry, i am new of jquery.i want to show the sub nav part when the mouse hover on the `li` part. but my code doesn't work.how to correct it. thank you My problem is relates to getting part of the text from a div, then pass this on to a variable which then acts upon it. This is a short summary in detail: http://jsfiddle.net/defencedog/W9Fsw/ Above simulates a Forum like environment (PHPBB3). here user can't insert html in posts thus a customised BBcode ([doc])is made in which is placed the required src attribute, unique to every SCRIBD document. The goal is to get the text within the two [doc] & that will be something like (discarding the two BBCodes) Code: src="http://www.scribd.com/embeds/2942002/content?start_page=1&view_mode=list&access_key=key-sxlnrfvqu5s22kzi5xb" then pass it on as a variable named src, which will then replace the contents to achieve desired output. Notice the two problems 1] how do I select the div upon which the action is to take place I.e that contains [doc]. Remember there occurs other divs as well in a posted message? 2] How do I select the desired text? Plz do giv me some suggestions I've got an idea for simplifying my code and avoid a bunch of switch functions but don't know how, though I assume it's possible. How do I write a function to change/insert only part of an img src? in other words something along the lines of if url contains: "/folder1/folder3/"; change this to: "/folder1/ folder2 /folder3/"; Hi, I have youtube link saved as a variable with value: PHP Code: http://www.youtube.com/watch?v=oOzuBOefL8I&feature=topvideos And I need to get a part of this link which I could use later: The part would be PHP Code: oOzuBOefL8I It would be great to do it with javascript... Because I have url generated like this one: PHP Code: httpObject.open("GET", "../talpinimas.php?id=reklama&paveikslelis=" +document.getElementById('paveikslelis').value+"&pavadinimas=" +document.getElementById('pavadinimas').value+"&url1=" +document.getElementById('url1').value+"&url1name=" +document.getElementById('url1name').value+"&komentaras=" +document.getElementById('komentaras').value+"&nick=<?php echo $nick?>" , true); and I want to send just THE PART (oOzuBOefL8I) to my talpinimas.php page. If you haven't understood something or need more info ask... Thanks 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 need to extract a particular part of a string: Code: var str=(document.images('flag').src); <img id="flag" src="../../../../img/ip/041-080/049p.png"> I only want the "041-080/049" part. It needs to be read from the right, not the left because the preceding left part will vary in length. How do I approach this? hello i need help in deleting a code part, like: table, ul ... the problem is that when i use rss feeds, lot of them add some code parts that i don't need like: <ul class="similar-topics">, and it shows similar topics so, i want to remove this code part from the rss feed i think that i may add a javascript code to the rss template that brings the rss feeds, so as it remove the part i define i hope you help me doing this, even with another idea another idea is to use: http://pipes.yahoo.com i"m sure there is a way to cut parts from the code, but i couldn't get it yet and i'm still trying Regards Hi This is for a class project, for some reason only the first part is working. The (sales*.20) and all of my else if are not. Kind of stuck on this. Code: <script type="text/javascript"> function bonus() { var sales var excellent var good var fair var poor sales= document.workBonus.txtSales.value; sales= parseInt(sales); excellent= document.workBonus.chkExc.value; good= document.workBonus.chkGood.value; fair= document.workBonus.chkFair.value; poor= document.workBonus.chkPoor.vaule; if (sales && excellent) { alert(sales * .20) } else if (sales && good) { alert(sales * .10) } else if (sales && fair) { alert(sales * .05) } else if (sales && poor) { alert(sales * .01) } } </script> Hello I needed an interdependent form for my website and found a JavaScript that would do the trick. Now i have no knowledge in JavaScript but i managed to set this up right. But i seems some part of the script makes a popup window which is showing what is chosen in the form and i reeeally want to remove that Can anyone pick out which piece of the script i need to remove? Thanks in advance Code: var ss2Values = [ // 'Please choose a subject' ['Please choose a category'], // '3D Printer' ['Please choose a category', 'Darwin', 'Huxley', 'MendelMax', 'Original Mendel', 'Printrbot', 'Prusa Mendel', 'Wallace', 'Upgrades'], // 'Home' ['Please choose a category', 'Bathroom', 'Bedroom', 'Decorations', 'Furniture', 'Home utilities', 'Household items', 'Kitchen', 'Livingroom', 'Outdoor', 'Spareparts'], // 'Mechanical' ['Please choose a category', 'Gears', 'Nuts and bolts', 'Tracks and bogies'], // 'Electrical' ['Please choose a category', 'Battery boxes', 'Terminal blocks', 'Outlet boxes'], // 'Electrical' ['Please choose a category', 'Architectural', 'Games', 'Ornaments', 'Toys'], // 'Educational' ['Please choose a category', 'Biological models', 'Chemical models', 'Mathematical', 'Physical models'] ]; window.onload = function() { var ss2 = new xSubSelect('sel20', 'sel21', null, ss2Values, ss2OnChange); } function ss1OnChange(s0, s1, s2) { alert( s0.options[s0.selectedIndex].value + ' / ' + s1.options[s1.selectedIndex].value + ' / ' + s2.options[s2.selectedIndex].value ); } function ss2OnChange(s0, s1) { alert( s0.options[s0.selectedIndex].value + ' / ' + s1.options[s1.selectedIndex].value ); } function xSubSelect(sSelId0, sSelId1, sSelId2, aValues, fnOnChange) { var s0 = document.getElementById(sSelId0); var s1 = document.getElementById(sSelId1); var s2 = sSelId2 ? document.getElementById(sSelId2) : null; if (s0 && s1) { s0.onchange = function() { var i, len, val; // clear existing options for s1 len = s1.options.length; for (i = 0; i < len; ++i) { s1.options[0] = null; } // insert new options for s1 len = aValues[s0.selectedIndex].length; for (i = 0; i < len; ++i) { val = aValues[s0.selectedIndex][i]; s1.options[i] = new Option(s2 ? val[0] : val); } // update s2 if (s2) { s1.onchange(); } }; if (s2) { s1.onchange = function() { var i, len; // clear existing options for s2 len = s2.options.length; for (i = 0; i < len; ++i) { s2.options[0] = null; } // insert new options for s2 len = aValues[s0.selectedIndex][s1.selectedIndex].length; for (i = 1; i < len; ++i) { s2.options[i - 1] = new Option(aValues[s0.selectedIndex][s1.selectedIndex][i]); } }; s2.onchange = function() { if (fnOnChange) { fnOnChange(s0, s1, s2); } }; } else { s1.onchange = function() { if (fnOnChange) { fnOnChange(s0, s1); } }; } s0.onchange(); // first init } } Hi, I have a bit of a problem and as my javascript is a rather poor I'm struggling to come up with a solution. Basically I need to split the variable "#country" into two parts and call the second half as the "this" value for '#int-dc'. Using '+' as the start point end ending at the end of the string. For instance 'this' returns 12+44 I want to split it to "12" and "+44" the latter (+44) replacing 'this' in the code below. Code: $(function() { $("#country").change(function() { $("#int-dc").val ($(this).val()); }); }); Thanks in advance. I have a situation in which I have to track clicks (links etc..). The problem is, I have a larger div ie - parentDiv - like 300px height, 400px width - approximate. This div has a background image. Now, the issue arises because over this div (parentDiv), I have another div "announceDiv" on top of it showing information. I ONLY want the parts of parentDiv that is NOT covered by announceDiv to be clickable. Just to reiterate, I have a big div that I want clickable (so I can add a link to the click), but over this big div I have a smaller div showing content ie, information, that links to its own stuff. BUT I want to be able to link from this background image that is in the parentDi without affecting when I click in the smaller div that is superimposed over it. any ideas? has to work in ie6+ and ff 3+ Hi all I am new here, hopefully someone can assist me with the following... If I have ../PACKAGES/test/run.html stored in the variable myUrl and I want to extract whatever is between ../PACKAGES/ and /run.html how would I go about it? I understand that in this instance I could use: var actualUrl = myUrl.slice(12,16); but the length of the string is dynamic, the only parts that stay the same are the start (../PACKAGES/) and end (/run.html). I am new to java-script, so please be kind...any assistance would be much appreciated. I'm trying to figure out if its possible to get only certain parts of a string like in PHP... If I have a function like this: Code: function getUrl(selectUrl) { The selectUrl look like this: index.php?page=test1 Now what I want is to only get a variable with "test1". Thast means I want to extract the page=test1 out of the selectUrl... Is this possible...? |