Good afternoon,

I need to display part of a url so that testers know which development site they are looking at.

At the moment I am using: <script language="javascript">document.writeln(document.location);</script> but this displays the whole url such as example:

http://development/B1888FG65789exampleexample376522003_7767389991122334453466737278685685847437

and would love it to be:

http://development/

Any ideas?

I'm not very good at regex so I won't attempt to answer with that but you can use simple string operations to achieve this. You could first split the location by "//" and then split the string again by /.

var loc = "http://development/B1888FG65789examp...78685685847437"; /* you would use document.location */
loc = loc.split("//");
var protocol = loc[0];
loc = loc[1];
var finalDisplay = protocol + "//" + loc.split("/")[0] + "/";

It's a bit messy but does what you want.

Thank you for this Scappedcola, I cant get it to work but I am guessing I am doing something wrong.

I have inserted the code in like this just above the previous code:

<script language="javascript">
	var loc = "document.location";
	loc = loc.split("//");
	var protocol = loc[0];
	loc = loc[1];
	var finalDisplay = protocol + "//" + loc.split("/")[0] + "/";
</script>

What am I doing wrong?

Don't put document.location in quotes. What you are assigning loc is a string containing the words document.location. Instead do:

var loc = document.location.toString();

Thank you for the quick reply Scappedcola, it is not working as of yet.

The code at the moment looks like this:

<script language="javascript">
	var loc = document.location.toString();
	loc = loc.split("//");
	var protocol = loc[0];
	loc = loc[1];
	var finalDisplay = protocol + "//" + loc.split("/")[0] + "/";
</script>

<div id="footer">You are using an <b><script language="javascript">

document.writeln(document.location);

</script> </b>version of this application </div>
div id="footer">You are using an <b><script language="javascript">
 
document.writeln(document.location);
 
</script> </b>version of this application </div>

In the above piece are you trying to display the entire URL or the shortened version we create at finalDisplay? If you want the shortened URL to display you need to

document.writeLn(finalDisplay);

Or you will only get the current full URL.

Fantastic it works! Thank you for your time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.