javascript/jquery add trailing slash to url (if not present)

javascript remove trailing slash
htaccess add trailing slash to specific url
javascript escape forward slash
java add trailing slash if missing
c# add trailing slash if missing
python append trailing slash
javascript remove slash from end of url
add trailing slash apache

I'm making a small web app in which a user enters a server URL from which it pulls a load of data with an AJAX request.

Since the user has to enter the URL manually, people generally forget the trailing slash, even though it's required (as some data is appended to the url entered). I need a way to check if the slash is present, and if not, add it.

This seems like a problem that jQuery would have a one-liner for, does anyone know how to do this or should I write a JS function for it?


Add End Trailing Slash To URL Using Jquery, You have to add slash if slash is not there in the end of url and my code is also won't work when any query string(?) is present in the url  You could always force a end trailing slash. How to add end trailing slash to URL using jquery: For adding the end slash you may write some jquery code. These code and it’s explanation are below. Step1: Open you code in any code editor. Step2: Paste the code in any master js file which is accessible to all pages. Jquery Code:


url += url.endsWith("/") ? "" : "/"

javascript/jquery add trailing slash to url (if not present), javascript remove trailing slash htaccess add trailing slash to specific url javascript get base url java add trailing slash if missing c# add trailing slash if missing function addSlashToUrl() { //If there is no trailing shash after the path in the url add it if ( window .location.pathname.endsWith ( '/') === false) { var url = window .location.protocol + '//' + window .location.host + window .location.pathname + '/' + window .location.search; window .history.replaceState ( null, document .title, url); } }


I added to the regex solution to accommodate query strings:

http://jsfiddle.net/hRheW/8/

url.replace(/\/?(\?|#|$)/, '/$1')

javascript/jquery add trailing slash to url (if not present), javascript/jquery add trailing slash to url (if not present). Question. I'm making a small web app in which a user enters a server URL from which it pulls a load of  Add Active Class to Selected Page (With and Without Trailing Slash) Tag: java , jquery , html , css , asp.net-mvc-4 I've got a selector that is adding the "Active" class to the selected page.


You can do something like:

var url = 'http://stackoverflow.com';

if (!url.match(/\/$/)) {
    url += '/';
}

Here's the proof: http://jsfiddle.net/matthewbj/FyLnH/

How to globally replace a forward slash in a JavaScript string , A regular expression is used to replace all the forward slashes. It will give an array of strings separated at the point where the forward slash was present. Adding a trailing slash to a url is unnecessary most of the times but comes to use in cases where you are working on a website that has been online for a couple of years and all the urls in the past used to be with a trailing slashes. Since, the urls with trailing slashes are already indexed by search engines, your client might not want to mess things up by submitting a new set of urls to search engines without trailing slashes.


Before finding this question and it's answers I created my own approach. I post it here as I don't see something similar.

function addSlashToUrl() {
    //If there is no trailing shash after the path in the url add it
    if (window.location.pathname.endsWith('/') === false) {
        var url = window.location.protocol + '//' + 
                window.location.host + 
                window.location.pathname + '/' + 
                window.location.search;

        window.history.replaceState(null, document.title, url);
    }
}

Return string without trailing slash, the URL string. javascript/jquery add trailing slash to url(if not present) (5) I'm making a small web app in which a user enters a server URL from which it pulls a​  IIS Url Rewrite appends trailing slash after page extension. I’m a big fan of IIS’s built in Url rewriting module, if you’re not using it, you should be to lowercase urls, set canonical urls and all sorts it’s great.


Trailing Slashes on Links, If not, are there any known workarounds that would allow me to use URLs without a trailing slash or file extension? I had a cursory glance at the jquery.mobile.​navigation.js code and the success callback I'm going to add some logic so that if this attribute exists on the data-role="page" div when it arrives,  It has nothing to do with the trailing slash. Sorry to go down the wrong path. If I comment out the php that includes the plugin js file, and the anchor jump works just fine.


URI.js - API Documentation, URI.js is a Javascript library for working with URLs. .origin() will be the same as .authority() (e.g. "example.org") if there is no scheme available. directory uri.​directory(); // returns string "/foo" (no trailing slash) // set directory uri.directory("/​bar"); .resource() will reset any of path, query and fragment if they're not specified. A regex works, but if you want to avoid that whole cryptic syntax, here's something that should work: javascript/jquery add trailing slash to url (if not present) var lastChar = url.substr(-1); // Selects the last character if (lastChar !== '/') { // If the last character is not a slash


HTML5, JavaScript, and jQuery 24-Hour Trainer, clicked can be found in the href attribute. This particular URL is referred to as a relative URL because it does not start with a forward slash or a domain name. Note that it’s best to leave a trailing slash in your URLs but in this case, I was comparing the location.href to a list of URLs from a database and I wanted the comparison to be consistent. How to Append to a File Using Sudo