Why does this link, which works when pasted into a url bar, not work when clicked?

414 Views Asked by At

I had this really strange problem that I figured out the solution to and I wanted to share it with the world, so I'm going to ask a question on here and answer it myself. Probably some other people will have better, more thorough answers than mine.

I have this link on a web page:

<a href="javascript:PreviewPersonalization('ShowGolfballPersonalization.asp?task=2&amp;ca=mixed&amp;t=1line&amp;c=%23FFCC00&amp;f=helvetica&amp;s=8pt&amp;l1=Test+Text&amp;l2=&amp;l3=&amp;logo=0')">Review Side 1 Personalization</a>

Here's the javascript function referenced in the link:

function PreviewPersonalization(pageDesc) {
window.open(pageDesc,'','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=750,height=650,left=0,top=0');
}

When I click the link, the function runs and opens the url in a separate window, and it runs that .asp page, but it produces a broken preview image, as if there were some problem with the url parameters.

When I copy the link text into the url bar of a separate window, and strip away the call to PreviewPersonalization, that works fine.

When I copy the link text into the JavaScript console and run it (stripping off the "javascript:" part of the link text so it just directly runs the PreviewPersonalization function, passing in the parameter that has been built into the link text), that also works fine.

So there's nothing wrong with the link text or the url parameters or the JavaScript function - So what is going on? Why doesn't clicking on the link work?

Stay tuned for the bizarre story..

1

There are 1 best solutions below

0
Shavais On

Debugging uncovered that the string passed to the PreviewPersonalization function had #FFCC00 instead of %23FFCC00. So the url was at that point invalid, so that none of the url parameters were making it into the code of ShowGolfBallPersonalization.asp.

The act of clicking on the link text was causing the browser to url decode the link text before processing it as JavaScript.

What the freak? The html doctype is 4.01, and I think there's "strict" mode mentioned somewhere, maybe that has something to do with it. But that is just bizarre to me. Anyway, having the JavaScript function re-url encode the url to be opened in a separate window fixes the problem.