JavaScript - Need To Extract Part Of A Url And Put It Into A Variable
I'm sure this is probably a simple question, but I'm going to ask anyway.
I need to extract part of a URL and place it into a variable for use elsewhere on the page. The part to be extracted will be of varying lengths. As an example, if I have the following URL: http://more.stltoday.com/stltoday/st...e?OpenDocument I need to extract "SS". Basically, anything that appears between "static.nsf/" and "/Steve's_APT_test_page". That value needs to be put into a variable and called elsewhere on the page. For testing purposes, I'm using the following code, just to see if it works (which, so far, it doesn't): Code: <script type= type="text/javascript" language="javascript"> var page_url = window.location.href; var segments = page_url.split("/"); alert(segments[4]); </script> Can anyone help? Thanks, Steve Similar TutorialsHi, I want to write some javascript code that uses arrays, I have a list of arrays named array1, array2, array3 etc. I want to perform a function on each of these arrays. I wanted to have a variable for the number after array so this is what I have: <code> var array1 = new Array ("1","2","3","4","5"); var array2 = new Array ("5","6","7","8","9"); for (var i=1; i<=2;i++){ document.writeln(("array"+i)[1]); } </code> the problem is it gives me the letters in the string array instead of the array values. How do I get javascript to treat ("array"+i) as array1? Hi all, I've a set of similar elements (to display thumbnails) having style attributes like Code: style="background:#ddd url('/images/photos/thumbs/89625045.jpg') top center no-repeat;" Code: style="background:#ddd url('/images/photos/thumbs/708905 copy.jpg') top center no-repeat;" etc. I need to get the background-image urls from the style (and then remove the part thumbs). My intention is to dynamically create an img element corresponding to each thumbnail image. I was thinking to use a substr() to get the urls and replace() to remove "thumbs", but the property style.backgrounImage gives a string like Code: url("/images/photos/thumbs/4923face.jpg") in FF3, where as in FF2 and IE6 it gives Code: url(/images/photos/thumbs/4923face.jpg) . How can I proceed with this? Any help would be apreciated. Thanks. I am working on a simple movie database to practise my JavaScript and have encountered difficulty extracting information from the xml file. Let me explain my scenario. Sorry if it gets a bit long-winded! The interface I have created has three columns which each serve the following functions: Column 1 -> The user selects a film of their choice. Column 2 -> Once the user has selected a film in Column 1 then more information about the film appears in this column. This includes title, director and cast. The user has the option of then selecting a cast member to find out information about them. Column 3 -> Once the user has selected a cast member in Column 2 then their name and a picture of them appears in this column (using the <img> tag). This information in this column also includes the film title, film artwork (applied <img> tag). But the difficulty I have encountered is as follows - I have a rough idea of how to update column 2 in real time to reflect the changes in Column 1. The method I am using for acquiring the relevant information in Column 2 is creating an array then using the indexOf to retrieve a the details of a specific film. I know this method is wrong and it would be better to pull the relevant information from the xml file. How do I use the idx from the Column 1 selection to pull out the relevant information to put in Column 2 and 3? Here is what I've done so far: Code: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> function XMLload() { jQuery.post(url, function(data) {getxml(data)}, 'xml'); } function dataFromTag(node, t) { var d = node.getElementsByTagName(t); if (d.length == 0) return (''); return (d[0].firstChild.nodeValue); } jQuery(document).ready(function() {XMLload();}); var url = 'movie.xml'; var xmlMovies; var aryMovieList = []; var xmlActors; var aryActors = []; var iframeOpen = '<html><head><\/head><body>' var iframeClose = '<\/select><\/form><\/body><\/html>' function getxml(xmldoc) { xmlMovies = xmldoc.getElementsByTagName('movie'); var hstr = iframeOpen; hstr += '<select size="' + xmlMovies.length + '" onchange="parent.actors(this.selectedIndex);">'; for (var MovieID = 0; MovieID < xmlMovies.length; MovieID++) { aryMovieList[aryMovieList.length] = dataFromTag(xmlMovies[MovieID], "title"); xmlActors = xmlMovies[MovieID].getElementsByTagName("actor"); for (var ActorID = 0; ActorID < xmlActors.length; ActorID++) { aryActors[aryActors.length] = dataFromTag(xmlMovies[MovieID], "director") + "/" + dataFromTag(xmlMovies[MovieID], "title") + "/" + dataFromTag(xmlActors[ActorID], "name"); } hstr += '<option>' + aryMovieList[MovieID] + '<\/option>'; } hstr += iframeClose; // test for aryMovieList // alert(aryMovieList); // test for aryActors // alert(aryActors); with(document.getElementById('movies').contentDocument) { open(); write(hstr); close(); } } function actors(idx) { var hstr = iframeOpen; var selectActor = []; hstr += 'title: ' + dataFromTag(xmlMovies[idx], 'title'); hstr += '<br>'; hstr += 'director: ' + dataFromTag(xmlMovies[idx], 'director'); hstr += '<br>'; for (var i = 0; i < aryActors.length; i++) { var aryActorList = aryActors[i].indexOf(dataFromTag(xmlMovies[idx], 'director') + '/' + dataFromTag(xmlMovies[idx], 'title')); if (aryActorList >= 0) { selectActor[selectActor.length] = i; } } // alert(selectActor); hstr += '<select size="' + selectActor.length + '" onchange="parent.info(this.selectedIndex);">'; for (var i = 0; i < aryActors.length; i++) { var aryActorList = aryActors[i].indexOf(dataFromTag(xmlMovies[idx], 'director') + '/' + dataFromTag(xmlMovies[idx], 'title')); if (aryActorList >= 0) { hstr += '<option>' + aryActors[i].substring(aryActors[i].lastIndexOf("/") + 1) + '<\/option>'; } } hstr += iframeClose; with(document.getElementById('actors').contentDocument) { open(); write(hstr); close(); } } function info(idx) { var hstr = iframeOpen; hstr += ''; hstr += iframeClose; with(document.getElementById('info').contentDocument) { open(); write(hstr); close(); } } </script> </head> Here is the XML file in use: Code: <movies> <movie> <title>Match Point</title> <director>Woody Allen</director> <image>Match-Point.jpg</image> <actor> <name>Scarlett Johansson</name> <image>Scarlett-Johansson.jpg</image> </actor> <actor> <name>Brian Cox</name> <image>Brian-Cox.jpg</image> </actor> <actor> <name>Matthew Goode</name> <image>Matthew-Goode.jpg</image> </actor> <actor> <name>Penelope Wilton</name> <image>Penelope-Wilton.jpg</image> </actor> </movie> <movie> <title>Inception</title> <director>Christopher Nolan</director> <artwork>Inception.jpg</artwork> <actor> <name>Leonardo DiCaprio</name> <image>Leonardo-DiCaprio.jpg</image> </actor> <actor> <name>Ken Watanabe</name> <image>Ken-Watanabe.jpg</image> </actor> <actor> <name>Joseph Gordon-Levitt</name> <image>Joseph-Gordon-Levitt.jpg</image> </actor> <actor> <name>Marion Cotillard</name> <image>Marion-Cotillard.jpg</image> </actor> <actor> <name>Ellen Page</name> <image>Ellen-Page.jpg</image> </actor> <actor> <name>Tom Hardy</name> <image>Tom-Hardy.jpg</image> </actor> </movie> <movie> <title>Blade II</title> <director>Guillermo del Toro</director> <artwork>Blade-II.jpg</artwork> <actor> <name>Wesley Snipes</name> <image>Wesley-Snipes.jpg</image> </actor> <actor> <name>Kris Kristofferson</name> <image>Kris-Kristofferson.jpg</image> </actor> <actor> <name>Ron Perlman</name> <image>Ron-Perlman.jpg</image> </actor> <actor> <name>Leonor Varela</name> <image>Leonor-Varela.jpg</image> </actor> <actor> <name>Norman Reedus</name> <image>Norman-Reedus.jpg</image> </actor> </movie> <movie> <title>Pulp Fiction</title> <director>Quentin Tarantino</director> <artwork>Pulp-Fiction.jpg</artwork> <actor> <name>John Travolta</name> <image>John-Travolta.jpg</image> </actor> <actor> <name>Samuel L Jackson</name> <image>Samuel-L-Jackson.jpg</image> </actor> <actor> <name>Uma Thurman</name> <image>Uma-Thurman.jpg</image> </actor> <actor> <name>Harvey Keitel</name> <image>Harvey-Keitel.jpg</image> </actor> </movie> <movie> <title>Avatar</title> <director>James Cameron</director> <artwork>Avatar.jpg</artwork> <actor> <name>Sam Worthington</name> <image>Sam-Worthington.jpg</image> </actor> <actor> <name>Zoe Saldana</name> <image>Zoe-Saldana.jpg</image> </actor> <actor> <name>Stephen Lang</name> <image>Stephen-Lang.jpg</image> </actor> <actor> <name>Michelle Rodriguez</name> <image>Michelle-Rodriguez.jpg</image> </actor> </movie> </movies> Many thanks in advance! If I wanted to extract certain parts of the URL... how can I do this? I know how to extract the entire URL or just the search query but what if i want to do this /boxscore.asp?w=2&s=4&yr= Just extract the numbers 2 and 4 from the url. I want to make a new link for this site I am on to plug those 2 numbers into here /game.asp?gnum=2&gslot=4 That's basically what i want the new window to read. Hi, I'm a beginner of JavaScript. I need the code for creating pgv_pvid and ssid. I had searched the web, and got some code, I wonder if the code is right: Code: var curDate = new Date(); var curMs = curDate.getUTCMilliseconds(); pgv_pvid = (Math.round(Math.random() * 2147483647) * curMs) % 10000000000; ssid = "s" + (Math.round(Math.random() * 2147483647) * curMs) % 10000000000; WScript.Echo(pgv_pvid) WScript.Echo(ssid) Hi, How I get the images tags from an HTML page with Javascript (SRSc)? Regards FG I want to type urls in a text field, submit such urls and extract pictures of these web pages. Does anyone know how I can do this using javascript? Thanks, Marcelo Brazil Why do I get both the greedy and non-greedy? How do I extract "somestring" only? I'm on IE7. thanks Code: <script type="text/javascript"> var x = "(EVAL)(H:somestring)Some other Text here"; var full =(x.match(/\(H\:(.*?)\)/g)); // produces "(H:somestring)" as expected alert(full); var inside = (x.match(/\(H\:(.*)\)/)); // produces "(H:somestring),somestring" .. I only want "somestring" alert(inside); </script> Hi, I currently have a asp.NET page which has a textbox with the ID: "txtbox1" I would like javascript to store the data entered into txtbox1 into a var. I have tried using var jVarName; jVarName = '<%#aVarName%>'; but so far it hasnt worked. Any suggestions would be appreciated ^^ Hi, I have an idea for putting news articles on my phone to read later when I have time. I'd like to be able to right-click the text of an article and choose "Send to Phone Queue." When I've got a few articles in the queue, I'd like to be able to bluetooth the file to my phone. I know some HMTL and CSS, but I'm very much a novice when it comes to JavaScript. I'm thinking that I probably can't even do a lot of this with JS because of it's limitations (i.e. writing to a file). Will I need to use PHP or Java for writing/bluetoothing the file? I was playing around with extracting the body-text of an article from FoxNews.com. I didn't get very far but you can see where I'm going with it. I welcome any comments, criticisms, etc. Thanks for anything you say, even shooting this down as unrealistic. Code: function getBodyText() { var divList = document.getElementsByTagName('div'); for ( i=0; i<=divList.length; i++) { if (divList[i].getAttribute("class") == "entry-content KonaBody") { . . . I have a string of text, and starting from a particular spot (known to be a number), I want to find the length of that number. Here's what I have: Code: ... var start = [...predetermined...] var end = 0; while (!isNaN(sourceCode.charAt(start + end))){ endCurrent++; } var myNum = sourceCode.substr(start, end) * 1; So let's say var sourceCode = "alkdjabjasdsdf-53 dnalsdb..."; , startCurrent will already be at the "5" and I want to be able to extract the "53". What I have works, but it seems cumbersome... Any advice? I am trying to redevelop firefox addon, to give it more funcionality. I found JS file where all the functions are and started to edit it. What I want to achieve is to get target of an anchor under mouse pointer (when mouse pointer is over anchor, I right click and call addon from context menu). For example when I have anchor which HTML code is: Code: <a href="somewehere.com/place">place</a> when I right click on this code and call my addon I would like to alert its href (somewehere.com/place) I wrote a function: Code: function ff() { var current_target=this.href; alert(current_target); } but it gives me udefined on alert Any hints to achieve this are highly appreciated. Thanks 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> 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/"; 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 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'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); } 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? 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...? |