Param passing issue

B

Brian

Hope someone can help .....

Have a function which creates a HTML HREF

function fLink(aText)
{ return '<a href=javascript:DoLinkSearch("'+aText+'")>'+aText+'</a>'; }
If the parameter value is "simple" (no spaces), then the function returns
the HTML as expected

However, if the value does contain spaces, the HTML returned is cut off at
the first space

eg

called with FRED returns
javascript:DoLinkSearch("FRED")>FRED</a>'

called with FRED SMITH returns javascript:DoLinkSearch("FRED

which is invalid HTML

Can anyone explain what is happening and how I "fix" the problem

Thanks,

Brian
 
M

McKirahan

Brian said:
Hope someone can help .....

Have a function which creates a HTML HREF

function fLink(aText)
{ return '<a href=javascript:DoLinkSearch("'+aText+'")>'+aText+'</a>'; }
If the parameter value is "simple" (no spaces), then the function returns
the HTML as expected

However, if the value does contain spaces, the HTML returned is cut off at
the first space

eg

called with FRED returns
javascript:DoLinkSearch("FRED")>FRED</a>'

called with FRED SMITH returns javascript:DoLinkSearch("FRED

which is invalid HTML

Can anyone explain what is happening and how I "fix" the problem

Thanks,

Brian

Use escape() as in:

function fLink(aText) {
return '<a
href=javascript:DoLinkSearch("'+escape(aText)+'")>'+aText+'</a>';
}
 
R

RobG

Brian said:
Hope someone can help .....

Have a function which creates a HTML HREF

function fLink(aText)
{ return '<a href=javascript:DoLinkSearch("'+aText+'")>'+aText+'</a>'; }
If the parameter value is "simple" (no spaces), then the function returns
the HTML as expected

However, if the value does contain spaces, the HTML returned is cut off at
the first space

eg

called with FRED returns
javascript:DoLinkSearch("FRED")>FRED</a>'

called with FRED SMITH returns javascript:DoLinkSearch("FRED

which is invalid HTML

The function will always return invalid HTML regardless of what
parameter you pass it. Error correction by the browser means that
sometimes it works if it's not too broken. Introducing spaces into the
parameter takes it beyond the error correction capabilities of most
browsers and you see the brokenness.

Attribute values (in this case the value of the A element's href
attribute) should be enclosed in quotes:

"In certain cases, authors may specify the value of an attribute
without any quotation marks. The attribute value may only contain
letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45),
periods (ASCII decimal 46), underscores (ASCII decimal 95), and
colons (ASCII decimal 58). We recommend using quotation marks even
when it is possible to eliminate them."

<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>

In the example, the inclusion of brackets () and quotes in the attribute
value means that it must have quotes. Single and double quotes have to
be properly nested and quoted:


return '<a href="javascript:DoLinkSearch(\''
+ aText + '\');">' + aText + '</a>';

Which gets really messy - why not use DOM (see below)?

Can anyone explain what is happening and how I "fix" the problem

Explanation above, the suggested 'fix' is just a bandaid.

Using script in the href attribute leads to mysterious behaviour for
some users so put something valid in the href and the script into an
onclick attribute that returns false to stop navigation.

The following will return an A element ready for insertion to your
document based on the same call you have now:

function fLink(aText)
{
var oA;
if (document.createElement && document.createTextNode){
oA = document.createElement('a');
oA.onclick = function (){
DoLinkSearch(aText);
return false;
}
oA.href = '#';
oA.appendChild(document.createTextNode(aText));
}
return oA;
}

In the calling function, make sure an element is returned and if so,
append it somewhere in the document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top