I have this code:
function getLocation() {
var siteurl = document.location.href;
document.write(siteurl);
}
I want to extract from string etc. "http://site.examle.com/page1/page2" this part:"/page1/page2".
How I can do it?
I have this code:
function getLocation() {
var siteurl = document.location.href;
document.write(siteurl);
}
I want to extract from string etc. "http://site.examle.com/page1/page2" this part:"/page1/page2".
How I can do it?
window.location.pathname
returns the string after the hostname. window.location is an object. If you're using firebug just type window.location in the console window and click on the output and you can see all of the properties you can access.
function getLocation() {
var siteurl = document.location.href;
// Remove '//' in above string so that we can easily extract our desired string based on '/' string. Doing so will not conflict with '//'.
var pStr = siteurl.replace("//","");
// Now extract substring from first index of '/' till last
var pathString = siteurl.substring(siteurl.indexOf('/'), siteurl.length);
document.write(siteurl);
// Now put pathSting on Document
document.write(pathString );
}
Here's another simple demo--on how you can strip keywords in the URL string.
<script type="text/javascript">
<!--
var siteurl;
var getLocation;
getLocation = function( url ) {
url = url.split(/[\s\,]+/i);
dummy = "http://site.example.com/page1/page2";
siteurl = String( location.href );
for ( var x = 0; x < url.length; x++ ) {
document.write( "Dummy URL String Matched " + (( x ) + 1 ) + ": " + (( dummy.match( url[ x ] ) === null ) ? String( url[x] + " Not found!" ).fontcolor("red") : dummy.match( url[ x ] )) + "<br>" );
document.write( "Document URL String Matched " + (( x ) + 1 ) + ": " + (( siteurl.match( url[ x ] ) === null ) ? String( url[x] + " Not found!").fontcolor("red") : siteurl.match( url[ x ] )) + "<br>" )
}
};
window.onload = getLocation("/page1, /page2, /page1/page2");
// You can split any keywords, using ( comma or space ).
// -->
</script>
Yes split is the anwser in this regard.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.