Steven
Steven

Reputation: 13995

jQuery append end of url?

I have a url variable http://blah.com/blah/blah/blah and I have another url http://shop.blah.com/ I want to take the first url (blah.com) and add the ending blah/blah/blah to the second url http://shop.blah.com

So I end up with http://shop.blah.com/blah/blah/blah

Any idea of how I could do this?

Upvotes: 0

Views: 704

Answers (4)

DefyGravity
DefyGravity

Reputation: 6031

    <script>
    $(document).ready(function(){
//this uses the browser to create an anchor
      var blah = document.createElement("a");
//initialize the anchor (all parts of the href are now initialized)
      blah.href = "http://blah.com/blah/blah/blah?moreBlah=helloWorld#hashMark";
      var shop = document.createElement("a");
//initialize the anchor (all parts of the href are now initialized)
      shop.href = "http://shop.blah.com/";
      shop.pathname = blah.pathname; //the blahs
      shop.search = blah.search; //the blah query 
      shop.hash = blah.hash;  // the blah hashMark
      alert("These are the droids you're looking for: "+shop.href); 
    });

    </script>

Upvotes: 0

Alnitak
Alnitak

Reputation: 340045

If the intent is just to add shop to the front of the domain name:

var url2 = url1.replace('://', '://shop.');

Upvotes: 0

rid
rid

Reputation: 63580

var url1 = 'http://blah.com/blah/blah/blah';
var url2 = 'http://shop.blah.com/';

var newUrl = url2 + url1.replace(/^.+?\..+?\//, '');

Upvotes: 1

James Johnson
James Johnson

Reputation: 46067

It sounds like the jQuery-URL-Parser plugin might come in handy here:

var url = $.url(); //retrieves current url

You can also get specific parts of the URL like this:

var file = $.url.attr("file");
var path = $.url.attr("path");
var host = $.url.attr("host");
...

If you need to get Querystring parameters:

var parm = $.url.param("id");

Upvotes: 0

Related Questions