Forum Moderators: open

Message Too Old, No Replies

Taking keyword content for your website from a URL

         

OnlineBud

6:18 am on Nov 17, 2004 (gmt 0)



Hey, I am not sure about how to put this question technically so I am just going to give an example...

What I want to do is take a url like this:

www.randomdomain.com/?keyword=My%20Random%20Domain

And in the HTML for this site if I put in some reference like &keyword& the keyword "My Random Domain" will be inserted as text onto my website.

I imagine there are quite a few ways this can be done but I figure javascript is the best.

Any suggestions would be greatly appreciated.

Thanks!

BlobFisk

9:11 am on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, OnlineBud!

Take a look at the script developed in this thread:

[webmasterworld.com...]

It should help you to do exactly what you are asking.

HTH

Bernard Marx

4:27 pm on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That thread seems to be about getting the filename from the URL.

To get information from search parameters, one good way is to parse the search string, and create an object with the keys as property names, and the values as ... values.

[pre]
function parseSearch()
{
var U = unescape ;
var pObj = new Object;
var pArr = window.location.search.substring(1).split(/[&=]/);
for(var k=0;k<pArr.length;k+=2)
pObj[U(pArr[k])] = U(pArr[k+1]);
return pObj;
}

[blue]// get object by parsing string[/blue]
var params = parseSearch();

[blue]// get the 'one' value[/blue]
alert(params.one)

[blue]// or enumerate to get all values[/blue]
ops = '';
for(var p in params)
ops += p+': '+params[p] + '\n'
alert(ops)
[/pre]

---------------------------------------------------------------

While on the subject of getting the filename, I thought that the solution offered in the thread quoted comes up a bit short, since it offers different versions for Windows and Unix. Many people will be writing on Windows and posting to Unix.

I reckon regular expressions should be brought into play. Here's my version:

docName = window.location.split(/[\/\\]/).pop().split('.')[0]

or, for non-popping browsers:

docName = window.location.replace(/.*[\/\\]¦\..*$/g,'')