开发者

How do I get only the name of a link without the file extension?

How do I get only the name of a link, e.g. "test" from "test.php"?

I'm trying to set up a variable:

var linkname= this.href.substring(2);

I don't know how substring() really works. Is this a开发者_JS百科 kind of the right approach?

Thanks.

UPDATE:

$(".nav").delegate("a", "click",function () {
  var linkname = this.href.substr( 0, ( this.href.length - '.php'.length ) );
  $("#claim"+linkname).fadeIn('slow').siblings().hide();
 });

First, thanks for the answer. But did I put in something wrong? This still doesn't work.

Maybe my fault: I need to remove a "#" at the beginning too.

UPDATE THREE

This the the complete code I'm working on:

$(document).ready(function(){

$("#claimone, #claimtwo, #claimthree").hide();

$("#claimone").show();

$(".nav").delegate("a", "click",function () {

    var linkname = this.href.substr( 0, ( this.href.length - '.php'.length ) );

    $("#claim"+linkname).fadeIn('slow').siblings().hide();

   });
});

The second function generates "#link.php". So I don't know if it doesn't collidate. All claims are in the index.php. By every new loaded content I want also to change the content. The links are like href="one.php", href="two.php", href="three.php".

$(function() {

    var newHash      = "",
        $mainContent = $("#maincontent-wrapper"),
        $pageWrap    = $("#wrapper"),
        baseHeight   = 0,
        $el;

    $pageWrap.height($pageWrap.height());
    baseHeight = $pageWrap.height() - $mainContent.height();

    $(".nav").delegate("a", "click", function() {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);

        if (newHash) {
            $mainContent
                .find(".maincontent")
                .fadeOut(200, function() {
                    $mainContent.hide().load(newHash + " .maincontent", function() {
                        $mainContent.fadeIn(200, function() {
                            $pageWrap.animate({
                                height: baseHeight + $mainContent.height() + "px"
                            });
                        });
                        $(".nav a").removeClass("active");
                        $(".nav a[href="+newHash+"]").addClass("active");
                    });
                });
        };

    });

    $(window).trigger('hashchange');

});

This is not an real solution, but it works. I just gave every href a .link class and a name for each claim.

$(".link").click(function () {  
    var divname= this.name;
    $("#claim"+divname).fadeIn(200).siblings().hide();
});

But that's exactly not what I wanted. Too much code. And now it's not synchron to the loaded content fade in. Would be better to combine both fadeins somehow.

FINAL SOLUTION

Far away form the first idea of solving this I managed it to bind it to the function it should have actually work with:

$claim
        .find("#claim")
                 .fadeOut(200, function() {
                    $claim.show().load(newHash + " #claim", function() {
                        $claim.hide().fadeIn(200);
   });
                });

Thanks everyone. Learned a bit more about subtrings beside!


var linkname = this.href.substr( '#'.length, ( this.href.length - '.php'.length ) );

'#' or '.php' can be altered to remove whatever you like from the beginning or the end of any string.


var linkname;
if ( this.href.indexOf('.') != -1 ) {
    linkname = this.href.substr(0, this.href.lastIndexOf('.'));
}
else {
    linkname = this.href;
}


You can remove a file extension from the end of a filename with this regular expression:

var url = this.href;   // test.php
url = url.replace(/\.[^\.\/]*$/, "");
alert(url);    // "test"

This will remove the characters from the last period to the end of the string and it will only do so in the last path element so you will get these results:

foo/test.php    ==> foo/test
test.php        ==> test
foo.x/test.php  ==> foo.x/test
foo.x/test      ==> foo.x/test
test.           ==> test
test            ==> test

Structurally, this regular expression is as follows:

a period 
followed by zero or more characters that is not a period and is not a slash 
up until the end of the string

You may also want to know that retrieving the href attribute for an <a> tag will retrieve a fully qualified URL regardless of what was in the original tag that starts with http:// or https:// and includes the domain and path. If you want to get only what was actually specified in the tag, you can use this.getAttribute('href') instead of this.href.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜