Jump to content

Recommended Posts

I have the following codes:

 

if(window.location.pathname.search(/\?/) != -1) 
{
    var refUrl = window.location.pathname.strip(/\?/);
}
else 
{
    var refUrl = window.location.pathname;
}


var pathname = refUrl += '/' + 'hey' + '/' + 'what';


alert(refUrl);

I'm running a check to see if there are any characters in the pathname, if so, strip it out. 

 

The output is : 

/_display//hey/what

So basically it didn't strip anything characters out from the pathname after testing this on jsfiddle. Can someone please tell me what's wrong with it?

 

Thank you,

halben

Edited by halben

Define what you mean by "character". Technically, everything in there is a character. Do you mean letters? Do you really want all the letters removed from the pathname? Perhaps you should tell us what you are really trying to do, since I can't think of a single reason you'd actually need to do that.

 

Also, location.pathname never has the domain in it, hence the naming convention "pathname".

 

Also, location.pathname should never have a query string (or hash/fragment string), hence the naming convention "pathname". so that if..else where you're looking to strip it is pointless.

 

So basically, you need to clarify what it is you are seeing that you want removed.

 

a) Explain exactly what characters you want removed,

b) Show a few "before" and "after" examples.

If I had to take a liberal guess, it sounds like what you want is

 

example url you are on:

 

http://www.mysite.com/path/to/page.html?foo=bar

 

what you want is "page.html".

 

var page = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
A note about this: There is no strict differentiation between "path" and "page name". As far as the server and the browser are concerned, the "page name" is just another part of the path. Now, things may recognize what that path ends with and do special things (like a path ending in ".php" might get parsed as a php file, etc.) but there is no reason you can't setup your server (or specify in browser script includes, etc.) something else.

 

For example, I can do this:

 

<script type='text/javascript' src='/path/to/blah'></script> 
and put javascript in the file "blah" that has no extension, and the browser will parse it just fine.

 

You will most commonly see this apparent like of page names in "beautified" or "seo friendly" urls (for example, look at the URL of this thread).

 

So what is my point? Well, mostly my point is for example about that last point: look at the URL of this thread. Running the code above will yield you an empty string. But you "probably" really want that "283243-keep-the-page-name-strip-the-url-of-the-domain-and-any-parameters" value, don't you.

So let's try this instead:

 

var page = location.pathname.replace(/^\/|\/$/g,'').split('/').pop();
But wait.. what if there's a Url like this:

 

http://www.yoursite.com/some/path/

 

and what's "really" happening is that you "really" have this:

 

http://www.yoursite.com/some/path/index.html

 

But your server is setup to automatically serve up that index.html in the absence of it? And again.. that's just a convention (in fact, a default on many servers). There's no reason someone can't make http://www.yoursite.com/some/path/ load up a file called "foo.bar" in that directory (or anywhere else on the server for that matter!). So now you may be beginning to see the problem here.. that there's no 100% guarantee that you're getting a page name because in reality, "page name" is one of those terms invented by the marketers for the lay-folk because they somehow thought that would help things.

 

So what is the real solution here? If you are writing code to be distributed, there is no real solution. This is especially painful in analytics when it comes to looking at site hierarchies/sections based on the URL. If however you are writing code only for your site, make sure your site has a standardized convention for calling something a "page" and then parse for that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.