Param passing issue

Param passing issue

Post by Bria » Sat, 04 Feb 2006 13:47:58


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
 
 
 

Param passing issue

Post by McKiraha » Sat, 04 Feb 2006 14:27:24


javascript:DoLinkSearch("FRED

Use escape() as in:

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

 
 
 

Param passing issue

Post by RobG » Sat, 04 Feb 2006 14:57:57


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.yqcomputer.com/ #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)?



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.


--
Rob
 
 
 

Param passing issue

Post by Jasen Bett » Sun, 05 Feb 2006 03:20:35


both examples are invalid HTML.


html needs "these quottes" around the whole href value.
also </ in a string is invalid javascript.


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



--

Bye.
Jasen