By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Param passing issue

P: n/a
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
Feb 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Brian" <br**********@esc.vic.gov.au> wrote in message
news:dr***********@otis.netspace.net.au...
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>';
}
Feb 3 '06 #2

P: n/a
Brian wrote:
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.
--
Rob
Feb 3 '06 #3

P: n/a
On 2006-02-03, Brian <br**********@esc.vic.gov.au> wrote:
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
both examples are invalid HTML.
Can anyone explain what is happening and how I "fix" the problem


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+'\')">'+aT ext+'<\/a>'; }

--

Bye.
Jasen
Feb 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.