Skip to content Skip to sidebar Skip to footer

Javascript - Append Text After URL Element

Is there a way to append something to a URL using javascript? and to remove prefixes? e.g. 'http://www.google.com/' into 'google.com.local/'

Solution 1:

You can change URL by using window-object, e.g.:

window.location.href = url + '/' + ID

Solution 2:

What about this?

var url = "http://www.google.com/";
url.replace(/^(https?:\/\/)?(\w+\.)?(\w+\.\w+)/, "$3.local");

DEMO: http://jsfiddle.net/qn5h5/


Solution 3:

The concat() method is used to join two or more strings.

This method does not change the existing strings, but returns a new string containing the text of the joined strings. In your case:

var mystring = concat("http://www.google.com",".local");

And for removing parts from string - use split() method to split a string into an array of substrings, and returns the new array.

Tip: If an empty string ("") is used as the separator, the string is split between each character.

Note: The split() method also does not change the original string.


Solution 4:

The javascript location object http://www.w3schools.com/jsref/obj_location.asp gives you access to url's hostname path etc. I don't exactly know what you are looking for, but this can help.


Post a Comment for "Javascript - Append Text After URL Element"