Forum Moderators: open

Message Too Old, No Replies

JavaScript is Automatically Terminating my URL?

JavaScript is adding a forward slash after the '.com' in a URL string!

         

skyeflye

5:41 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



ARGH!

I can not figure this out. It seems like this snippet of JavaScript code has a mind of its own!

I am building a hyperlink with JavaScript that, when clicked, will take the user directly to a Google Search. The query string for the google search will be the current page's title. For example, if the page's title was "Blue Socks", then the href I want to build would be:
[google.com?q=Blue+Socks...]

The problem is, for some unknown reason, the script I wrote (below) keeps creating the link like this instead:

[google.com...]

See the extra slash before the question mark? Why? Why? Why is that there? It's as if the JavaScript is 'terminating' the domain name, all by itself. This is so annoying.

Here's the code I wrote. You'll need to paste it onto a real HTML page in order to experience the annoying behavior mentioned above.


<script language="JavaScript">

// Create a new String() object called page_title
// We create an object like this so we can use the replace() method on it below.
var page_title = new String();

// assign the value of the document's title to the page_title variable.
page_title = document.title;

// Create the query_string from the page_title by replacing spaces with plus signs.
var query_string = page_title.replace(' ', '+');

// Define the search URL
var search_url = 'http://www.google.com?q=' + query_string;

// I don't know why this is inserting a DAMN forward slash before the '?'
// This is bugging me to no end!
document.write ('<a href="' + search_url + '" target="_blank">Click Me</a>');

</script>


I am totally lost on this. I can't find any reference to this kind of behavior anywhere. I hope soebody can come up with a workaround for this.

I tried a bunch of things, like splitting up the string into smaller chunks before document.write'ing it. Nothing seems to make any difference. Please help! Thanks!

DrDoc

6:17 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Which browser is adding the slash? None of the Windows' browsers do.
There is no way the JavaScript would know that your string holds a URL. And, even if it did, it wouldn't add a slash by itself. Or, are you talking about the Google error you get when clicking on the link? If so -- that's on Google's end (and nothing you could do anything about). However, the reason you get the Google error in the first place is because the Google search URL is not correct. See my changes below (in bold) :)

<script type="text/javascript">
// Create a new String() object called page_title
// We create an object like this so we can use the replace() method on it below.
var page_title = new String();

// assign the value of the document's title to the page_title variable.
page_title = document.title;

// Create the query_string from the page_title by replacing spaces with plus signs.
var query_string = page_title.replace(' ', '+');

// Define the search URL
var search_url = 'http://www.google.com/search?q=' + query_string;

// I don't know why this is inserting a DAMN forward slash before the '?'
// This is bugging me to no end!

document.write ('<a href="' + search_url + '" target="_blank">Click Me</a>');
</script>

Neat functionality ;) Creative approach :)

DrDoc

6:21 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While we're at it... your script can be shortened down to only one line:

<script type="text/javascript">
document.write("<a href=\"http://www.google.com/search?q=" + document.title.replace(' ', '+') + "\">Click Me</a>");
</script>

skyeflye

8:40 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Thanks very much for the help!

Yeah, I was making the script really easy to read for someone else who is just learning how to code. That's why it was on so many lines. Thanks for the 'compression'. :)

I now see what my problem was. It was actually Google's web server that was adding the slash...trying to make some sense of the corrupt URL I was providing. Sweet!

That's cool...I'll just implement the change you made, and that fixes the problem completely.

I knew it had to be something simple. Thanks much for the help!

Woohoo!

drbrain

8:58 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use escapeURI() not replace to escape the query string. It won't properly encode a request string like "beer & nuts" or "&oe=SJIS".

skyeflye

9:08 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Oh OK...that's great info too!

I've been coding PHP for so long, I totally forgot all the JavaScript I knew eons ago...and back then I was never messing around with query strings and URIs anyway...all DHTML...in the early days of it too.

That's great...Thanks!