tags:

views:

39

answers:

4

Hi Guys,

I am using Jquery.

I have got below links in my html

<a class="load-fragment" href="/english/india/index.aspx"><span>Overview</span></a>
<a class="load-fragment" href="/english/india/guide.aspx"><span>Guide</span></a>

I am trying to get out "index" and "guide" from (href="/english/india/index.aspx", href="/english/india/guide.aspx") text from above links and they will be stored in a variable for further use, there will be looping as there can be many links as given above.

Please suggest using Jquery!

Thanks!

+1  A: 

How about this?

var filenames = $('.load-fragment').map(function() {
   var href =    $(this).attr('href'); 
   var lastSlash = href.lastIndexOf('/');
   var new_href = href.substring(0, lastSlash);
   return href.substring(lastSlash + 1, href.lastIndexOf('.')));
}).get();​
Jacob Relkin
Jacod, can I please request you to please suggest on this question http://stackoverflow.com/questions/3952465/loading-page-fragments-on-main-page-using-jquery. It would be great help as I am stuck due to that
MKS
A: 

I would do something like this:

var files = [];
$('a.load-fragment').each(function(){
    var loc = $(this).attr('href');
    loc = loc.substring(loc.lastIndexOf('/')+1);
    files.push(loc);
});
alert(files);

You can then access each of them from the files array, files[0], files[1], etc.

WSkid
You've left the ".aspx" on them.
T.J. Crowder
+3  A: 

You could use a regex like this:

$(".load-fragment").each(function() {
   var fileName = $(this).attr("href").match(/^\/.*\/([^\/]+).aspx$/)[1];
   //store fileName where needed
});​

You can test it out here, or if say you wanted an array of them use .map():

var files = $(".load-fragment").map(function() {
              return $(this).attr("href").match(/^\/.*\/([^\/]+).aspx$/)[1];
            }).get();​
Nick Craver
This is cool, although it won't work if the file extension is not `.aspx` :P
Jacob Relkin
@Jacob - Easy adjustment if needed, though given what's *usually* the case, it's probably highly unlikely that his fragment loads are anything *but* `.aspx`...looks like a vanilla ASP.Net WebForms use here.
Nick Craver
Many Thanks for your reply Nick, it worked for me, can I please ask you to look in my another question and suggest how to proceed on that http://stackoverflow.com/questions/3952465/loading-page-fragments-on-main-page-using-jquery ..sorry for this, would appreciate if you can please suggest on that question also
MKS
Can you please also suggest how to use .load on newly created div, as I am looking to load page fragments from other pages, please the code to get dynamic div var newDiv = $("<div>").attr("id",dynDivID); //adding div with above taken ID $("#column2").append(newDiv);
MKS
@Solution - Your question is very unclear, what are you trying to do, how are they being created?
Nick Craver
I am trying to create dynamic divs on page load and then further I need to load page fragment taken from above links, can you please comment on that question so that I can modify it according to show you
MKS
A: 

A variant of Nick Craver's answer:

$(".load-fragment").each(function() {
   var fileName = this.href.split(/[/.]/).slice(-2,-1);
   //store fileName where needed
});

However, this assumes the links always have the format you show. The regex may be safer since it also is validation.

"/english/india/index.aspx".split(/[/.]/)
// returns: ["", "english", "india", "index", "aspx"]

["", "english", "india", "index", "aspx"].slice(-2, -1)
// returns "index"
Tomalak