[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 679
  • Last Modified:

Javascript to write SEF URLs

I'm trying to put together a javascript that updates a form hidden input with search engine friendly urls based upon the page title.

My page has the following -

A recordset detailing Parent details -The page may or may not have a parent page, if it does the recordset creates an array 'arrParentDetail' of which the third item is the Parent Page SEF URL. (I'll refer to this as 'Parent-Detail-SEF')

In the event that this exists, the SEF URL for the new page would be -


So in this instance the hidden input should have a value equal to 'Parent-Detail-SEF' +'/' + The New Page Title

The New Page Title should remove all non a-z A-Z 0-9 characters and replace all spaces with a '-' So
New! Page About&* Something Special :) would become -


In the event that the arrParentDetail doesn't exist the hidden input would be just -


my page title input has a name and id of title.

Finally I also need to be able to write the final output to a span so that the user can see what the URL will be.

I'd be grateful for some assistance with this please.

Thank you

without the preceeding '/'
  • 2
1 Solution
Hi, I made the assumption that you want to update the hidden field everytime the title field changes.
Also in the example I have the hidden field not hidden, but as plain text, such that later after verification you can change type="text" to type="hidden" yourself.

		<script type="text/javascript">
			var arrPageDetails = ['something', 'something', 'Parent-Detail-SEF'];

			function updateSEF(frm) {
				var parts = [];

				// In case of parent SEF, add this to the new page's SEFURL
				if (typeof arrPageDetails !== "undefined" && arrPageDetails.length > 2) {
				// Grab the title field as inputed by the end user
				var pageTitle = frm.title.value;
				// Remove leading and trailing invalid characters
				pageTitle = pageTitle.replace(/^\W+|\W+$/ig,'');

				// Replace all non alphanumerics to dashes.

				// Construct the final url, and write this back to the form.
				frm.SEFUrl.value = parts.join("/");
			<input type="text" name="SEFUrl" value="" />
					<input type="text" name="title" id="title" onchange="(updateSEF(this.form));" />
			<input type="submit" value="Submit form" />

Open in new window

garethtnashAuthor Commented:
Thanks Roonaan, that looks fantastic, just a couple of things,

my Array is created in VBScript -

Dim ParentDetail__MMColParam
ParentDetail__MMColParam = NULL
If (Request("parent") <> "") Then 
  ParentDetail__MMColParam = Request("parent")
End If

Dim ParentDetail
Dim ParentDetail_cmd
Dim ParentDetail_numRows

Set ParentDetail_cmd = Server.CreateObject ("ADODB.Command")
ParentDetail_cmd.ActiveConnection = MM_GolfConnection_STRING
ParentDetail_cmd.CommandText = "SELECT ID, PageType, SEOLink, Title FROM dbo.MyPage WHERE ID = ?" 
ParentDetail_cmd.Prepared = true
ParentDetail_cmd.Parameters.Append ParentDetail_cmd.CreateParameter("param1", 5, 1, -1, ParentDetail__MMColParam) ' adDouble

Set ParentDetail = ParentDetail_cmd.Execute
ParentDetail_numRows = 0

If not ParentDetail.Eof then
dim arrParentDetail
arrParentDetail = ParentDetail.GetRows()
End if

Set ParentDetail = Nothing
Set ParentDetail_cmd = Nothing

Open in new window

I can create a variable in VBScript from this like

Dim Purl
Purl = arrParentDetail(2,0)instead of creating the code like -

instead of the 'var arrPageDetails = ['something', 'something', 'Parent-Detail-SEF'];'

Can I use a variable created in Vbscript within the Javascript? or would it be better to store the value in a hidden input?

Also sorry, when I finished typing in title the value was displayed in SEFUrl but only the first space was replaced, in other words this 'another url created' looked like 'another-url created'

Is there a work around for this?

Thank you so much
garethtnashAuthor Commented:
Thank you

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now